I have a form through which users can upload an image. On my home machine I was validating like so:
if(isset($_FILES['image']['name']))
And it was working fine, but it fails on my work mach开发者_JS百科ine. I need to use:
$_FILES['image']['name'] != ''
I've tried empty($_FILES['image']), but this doesn't work either. I was just wondering why this would be the case?
Even when $_FILES['photo']
is set, you should check $_FILES['photo']['error']
for exceptions such as partial uploads (UPLOAD_ERR_PARTIAL
) or empty uploads (UPLOAD_ERR_NO_FILE
) as explained in the Handling file uploads section of the PHP Manual.
The ['name']
might be unset or empty because POST forms are sent as multipart/form-data
request type. And the filename=
attribute is allowed to be empty. I'm not sure, but browsers might very well be configured (did you test with MSIE at work perchance?) to suppress it for security reasons. It's not an reliable field anyway.
This is no explanation why the $_FILES["image"]
however was empty. I'd also surmise an upload error there. But anyway, you should be testing with print_r($_FILES);
.
Also check the manual section on common pitfalls http://php.net/manual/en/features.file-upload.common-pitfalls.php regarding configuration settings.
精彩评论