为什么我的表单只接受 .jpeg 文件
我制作了这个表单:
<form enctype="multipart/form-data" action="php/handler.php" method="post" name="uploaderform">
<table>
<tr><td align="right">File:</td><td><input type="hidden" name="MAX_FILE_SIZE" value="30000"/><input type="file" name="filetoup" id="zzz"/></td></tr>
<tr><td align="right">Folder:</td><td><select id="category" name="category"><option value="foo/">some</option><option value="poo/">some2</option></select></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
和这个 PHP 脚本:
if((exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_GIF)
||(exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_JPEG)
||(exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_PNG)
&&($_FILES["filetoup"]["size"]<2000000)){
if($_FILES["filetoup"]["error"]>0)
echo $_FILES["filetoup"]["error"];
else {
if(isset($_FILES["filetoup"]["name"]))
$target_path=$folder . basename($_FILES["filetoup"]["name"]);
else
echo "Choose a file";
if(move_uploaded_file($_FILES["filetoup"]["tmp_name"],$target_path))
echo $_FILES["filetoup"]["name"] . "uploaded";
else
echo "Either you fucked up or I did";
}
}else{
echo "read whats under the form";
}
我只能上传 .jpeg 文件,不接受 .gif 和 .png,我收到此错误:
Warning: exif_imagetype() [function.exif-imagetype]: Filename cannot be empty in C:\xampp\htdocs\atthepc\php\handler.php on line 7
我认为这是我表单中的内容,因为我可以上传 .jpeg 而无需任何错误,但 .gif 和 .png 没有。如果我不对,那是什么原因造成的呢?有解决办法吗还是我做的一切都错了?
最后,.jpg 是否被识别为 .jpeg?
/编辑
我认为我解释得不正确,如果我尝试上传 jpeg 文件,它工作正常,但如果我尝试任何其他文件类型,它会给我文件名不能为空,所以我的表单不想接受 gif 并发送通过帖子,就像帖子是空的一样。为什么会发生这种情况?
//编辑 是的,就是这样,我尝试了这个
$something=$_FILES["filetoup"]["tmp_name"];
var_dump($something);
,它给了我 string(0) "" 。表格不想接受它,但为什么呢?我没有在表格中设置任何限制。
///编辑
我发现了我的问题,它不接受任何其他文件类型,因为我试图上传的文件太大(gif动画)并且max_file_size太小。
I made this form:
<form enctype="multipart/form-data" action="php/handler.php" method="post" name="uploaderform">
<table>
<tr><td align="right">File:</td><td><input type="hidden" name="MAX_FILE_SIZE" value="30000"/><input type="file" name="filetoup" id="zzz"/></td></tr>
<tr><td align="right">Folder:</td><td><select id="category" name="category"><option value="foo/">some</option><option value="poo/">some2</option></select></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
and this PHP script:
if((exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_GIF)
||(exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_JPEG)
||(exif_imagetype($_FILES["filetoup"]["tmp_name"])==IMAGETYPE_PNG)
&&($_FILES["filetoup"]["size"]<2000000)){
if($_FILES["filetoup"]["error"]>0)
echo $_FILES["filetoup"]["error"];
else {
if(isset($_FILES["filetoup"]["name"]))
$target_path=$folder . basename($_FILES["filetoup"]["name"]);
else
echo "Choose a file";
if(move_uploaded_file($_FILES["filetoup"]["tmp_name"],$target_path))
echo $_FILES["filetoup"]["name"] . "uploaded";
else
echo "Either you fucked up or I did";
}
}else{
echo "read whats under the form";
}
and I can only upload .jpeg files, .gif and .png are not accepted and I get this error:
Warning: exif_imagetype() [function.exif-imagetype]: Filename cannot be empty in C:\xampp\htdocs\atthepc\php\handler.php on line 7
I think that it's something from my form, because I can upload .jpeg without any errors but .gif and .png not. If I'm not right, what then is causing this? And is there a solution or am I doing everything wrong?
And finally, are .jpg recognized as .jpeg's?
/edit
i dont think i explained myself correctly, if i try to upload a jpeg file, it works fine but if i try any other file type it gives me Filename cannot be empty in , so my form doesnt want to accept the gif and send it through post, its like the post is empty. why is thsi happening?
//edit
and yes that is it, i tried this
$something=$_FILES["filetoup"]["tmp_name"];
var_dump($something);
and it gives me string(0) "" . the form doesnt want to accept it but why? i did not put any restriction in the form.
///edit
i found my problem, it was not accepting any other file type because what i was trying to upload was too big(animated gif) and the max_file_size was too little.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许尝试将类型与 IF 语句进行比较,而不是 tmp_name 与 exif_imagetype 进行比较? $_FILES["filetoup"]["type"])==IMAGETYPE_GIF
另外,关于 JPEG,w3schools。 com提到:“注意:对于IE识别jpg文件,类型必须是pjpeg,对于FireFox,它必须是jpeg。”所以也许这是值得研究的事情
maybe try comparing type with an IF statement instead of tmp_name with exif_imagetype? $_FILES["filetoup"]["type"])==IMAGETYPE_GIF
Also, re JPEG, w3schools.com mentions that:"Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg." so maybe that's something to look into
使用包含所有 if 的 switch case 并在每个 jpeg、png 和 gif 的情况下设置 $target_path(如果它们需要位于不同的文件夹中)。如果不允许 tmp 图像文件,则将默认设置为错误。
只是一种方法,希望有帮助
use a switch case insted of all the if's and set the $target_path in the case for each jpeg, png and gif, if they needs to be in different folders.. and make the default as an error if tmp image file aint allowed..
just one way to do it, hope it helps