问题
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.
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);)
Anyone have any ideas on what the cause of this is?
回答1:
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
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;
}
回答2:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in
Verify the MAX_SIZE variable, example to define it for a 2Mo image:
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 = ?
回答3:
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.
upload_max_filesize = 500M
post_max_size = 500M
max_execution_time = 120
回答4:
you should change the form enctype to "multipart/form-data"
<form method="post" enctype="multipart/form-data">
....
</form>
回答5:
After you upgrade 'upload-max-filesize' , Restart all Services of wamp or xampp ! it will right !
回答6:
Notice: Undefined index: fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 38
Notice: Undefined index: fileToUpload in D:\xampp\htdocs\public_html\php\form_php\application\process_application.php on line 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.
If You Get Errors like this one you should check you have added form enctype to "multipart/form-data"
<form method="post" enctype="multipart/form-data">
....
</form>
Use to catch me out quite a lot to begin with.
来源:https://stackoverflow.com/questions/16043892/warning-getimagesize-function-getimagesize-filename-cannot-be-empty-warnin