php 警告:getimagesize() [function.getimagesize]:文件名不能为空警告消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16043892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty warning message?
提问by Ajmal Tk
Up until recently I've been using some PHP to upload photos to a site. But suddenly it's started triggering all sorts of error messages.
直到最近,我一直在使用一些 PHP 将照片上传到网站。但突然间它开始触发各种错误消息。
I use a form that on action runs the following code:
我使用一个在行动中运行以下代码的表单:
$uploaddir = "../../user_content/photo/";
$allowed_ext = array("jpeg", "jpg", "gif", "png");
if(isset($_POST["submit"])) {
$file_temp = $_FILES['file']['tmp_name'];
$info = getimagesize($file_temp);
} else {
print "File not sent to server succesfully!";
exit;
}
The file upload part of the form has the following elements:
表单的文件上传部分具有以下元素:
<input name="file" type="file" class="photo_file_upload">
The submit button for uploading has the following attributes:
用于上传的提交按钮具有以下属性:
<button type="submit" name="submit" class="photo_upload">Upload</button>
But whenever I run this, I always get the following warning:
但是每当我运行它时,我总是收到以下警告:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in (upload PHP file) on line 10
(line 10 is this part: $info = getimagesize($file_temp);)
(第 10 行是这部分:$info = getimagesize($file_temp);)
Anyone have any ideas on what the cause of this is?
任何人都对这是什么原因有任何想法?
回答by Yaroslav
You checked if the form was submitted, but didn't check if the file was sent. In some cases, a form could be submitted but the file will not sent (i.e. file size is bigger then file size limit in config).
您检查了表单是否已提交,但未检查文件是否已发送。在某些情况下,可以提交表单但不会发送文件(即文件大小大于配置中的文件大小限制)。
Use this:
用这个:
if(isset($_POST["submit"]) && isset($_FILES['file'])) {
$file_temp = $_FILES['file']['tmp_name'];
$info = getimagesize($file_temp);
} else {
print "File not sent to server succesfully!";
exit;
}
You see && isset($_FILES['file'])is new
你看到&& isset($_FILES['file'])是新的
Or you can extend it
或者你可以扩展它
if(isset($_POST["submit"]) && isset($_FILES['file'])) {
$file_temp = $_FILES['file']['tmp_name'];
$info = getimagesize($file_temp);
}
elseif(isset($_POST["submit"]) && !isset($_FILES['file'])) {
print "Form was submitted but file wasn't send";
exit;
}
else {
print "Form wasn't submitted!";
exit;
}
回答by Vincent
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in
警告:getimagesize() [function.getimagesize]:文件名不能为空
Verify the MAX_SIZE variable, example to define it for a 2Mo image:
验证 MAX_SIZE 变量,例如为 2Mo 图像定义它:
define('MAX_SIZE', 2000000);
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_SIZE; ?>" />
<input name="fichier" type="file" id="fichier_a_uploader" /> <br>
<input type="submit" name="submit" value="Uploader" />
</form>
you can also verify the Maximum size of POST data that PHP will accept and Maximum allowed size for uploaded files in PHP.ini : post_max_size = ? upload_max_filesize = ?
您还可以在 PHP.ini 中验证 PHP 将接受的 POST 数据的最大大小和上传文件的最大允许大小: post_max_size = ? 上传最大文件大小 = ?
回答by Ajmal Tk
Change these parameters in your php.ini file; (Currently it allow only 2Mb size to upload and post). Sometimes it's reason for these errors.
在 php.ini 文件中更改这些参数;(目前它只允许上传和发布 2Mb 大小)。有时这是这些错误的原因。
upload_max_filesize = 500M
post_max_size = 500M
max_execution_time = 120
回答by Mahdi Mousavi
After you upgrade 'upload-max-filesize' , Restart all Services of wamp or xampp ! it will right !
升级 'upload-max-filesize' 后,重启 wamp 或 xampp 的所有服务!它会正确的!
回答by Dinu
you should change the form enctype to "multipart/form-data"
您应该将表单 enctype 更改为“multipart/form-data”
<form method="post" enctype="multipart/form-data">
....
</form>
回答by L'equipe Gaming
Notice: Undefined index: fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 38
注意:未定义索引:fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php 第 38 行
Notice: Undefined index: fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 43
注意:未定义索引:fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php 第 43 行
Warning: getimagesize(): Filename cannot be empty in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 43 File is not an image.Sorry, file already exists. Notice: Undefined index: fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 58 Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.
警告:getimagesize():第 43 行 D:\xampp\htdocs\public_html\php\form_php\application\process_application.php 中的文件名不能为空 文件不是图像。抱歉,文件已存在。注意:未定义索引:fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php 第 58 行 抱歉,只允许 JPG、JPEG、PNG 和 GIF 文件。抱歉,您的文件没有上传。
If You Get Errors like this one you should check you have added form enctype to "multipart/form-data"
如果您遇到这样的错误,您应该检查您是否已将表单 enctype 添加到“multipart/form-data”
<form method="post" enctype="multipart/form-data">
....
</form>
Use to catch me out quite a lot to begin with.
开始时经常用它来抓住我。