[英]PHP - uploading images and $_files variable
I'm creating a blog with a WYSIWYG editor for blog posts as well as an image upload for each blog post.我正在为博客文章创建一个带有 WYSIWYG 编辑器的博客,并为每篇博客文章上传图片。
So my inputs look something like this, I'm including a file here called wysiwyg.php
but this isn't really necessary for this question.所以我的输入看起来像这样,我在这里包含了一个名为
wysiwyg.php
的文件,但这对于这个问题并不是真正必要的。 (It basically has buttons for bold italics etc, and it's just included like this because I use it in other areas of my site) (它基本上有用于粗斜体等的按钮,它只是这样包含,因为我在我网站的其他区域使用它)
<form action="blog_create.php" method="POST">
<input id="title" name="title" type="text" placeholder="Title">
<input id="post_image" name="post_image" type="file">
<textarea id="description" name="description" placeholder="Description"></textarea>
<textarea id="meta" name="meta" placeholder="Meta"></textarea>
<div id="wysiwyg_controls"><?php include_once("wysiwyg.php"); ?></div>
<iframe name="richTextField" id="wysiwyg" class="news_article"></iframe>
<input type="hidden" id="text_content" name="text_content" value="">
<input type="hidden" id="save_as_type" name="save_as_type" value="">
<button id="blog_draft_submit" name="blog_draft_submit" onclick="return false">Save as Draft</button>
<button id="blog_publish_submit" name="blog_publish_submit" onclick="return false">Publish</button>
</form>
I then use JavaScript/jQuery to submit the form like so to actually get the content of the iframe:然后我使用 JavaScript/jQuery 像这样提交表单以实际获取 iframe 的内容:
$("#blog_draft_submit").on("click", function(){
$text_content = richTextField.document.getElementsByTagName('body')[0].innerHTML;
$("#text_content").attr("value", $text_content);
$("#save_as_type").attr("value", "draft");
this.form.submit();
});
$("#blog_publish_submit").on("click", function(){
$text_content = richTextField.document.getElementsByTagName('body')[0].innerHTML;
$("#text_content").attr("value", $text_content);
$("#save_as_type").attr("value", "publish");
this.form.submit();
});
However, this doesn't seem to be working properly for the file input or being able to access anything that should be in the Global $_FILES
variable.但是,这似乎不适用于文件输入或无法访问应在 Global
$_FILES
变量中的任何内容。
If I do var_dump($_FILES);
如果我做
var_dump($_FILES);
I get:我得到:
array (size=0)
empty
But when I do var_dump($_POST);
但是当我做
var_dump($_POST);
I get:我得到:
array (size=8)
'title' => string '' (length=0)
'post_image' => string 'image.png' (length=23)
'description' => string '' (length=0)
'meta' => string '' (length=0)
'wysiwyg_image' => string '' (length=0)
'text_content' => string '' (length=0)
'save_as_type' => string 'draft' (length=5)
Is this because of the way I am submitting via JS or am I actually doing something wrong?这是因为我通过 JS 提交的方式还是我实际上做错了什么? Will I need to create a seperate parsing file and do an
XMLHttpRequest
and something AJAX-like?我是否需要创建一个单独的解析文件并执行
XMLHttpRequest
和类似 AJAX 的操作?
AJAX is not needed.不需要 AJAX。
Try adding enctype="multipart/form-data"
in <form>
tag尝试在
<form>
标签中添加enctype="multipart/form-data"
Some rules to follow when we use are working with $_FILE
:当我们使用
$_FILE
时要遵循的一些规则:
- Check if the
file_uploads = On
on the php.ini file检查 php.ini 文件上的
file_uploads = On
- Make sure that the form uses method="post"
确保表单使用 method="post"
- The form also needs the following attribute:
enctype="multipart/form-data"
( It specifies which content-type to use when submitting the form )表单还需要以下属性:
enctype="multipart/form-data"
(它指定提交表单时使用的内容类型)
Without the requirements above, the file upload will not work.如果没有上述要求,文件上传将无法进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.