PHP文件上传中的文件无效 [英] Invalid File on PHP File Upload

查看:94
本文介绍了PHP文件上传中的文件无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用HTML表单/PHP上传了一个用于图像文件的网站,当它在本地计算机上的XAMPP上运行时,它运行良好,但是在大多数情况下,当我将其上传到000webhost时,它说无效文件,并且只有在某些情况下,图像才能成功上传.我试图在php配置中调高最大执行时间,但这似乎并没有解决.我尝试上传的文件小于php配置中的最大文件大小,并且可以在我的测试机上完美运行.

I've built a website with a HTML form/ PHP upload for image files, it works well when its running on XAMPP on my local computer but when i've uploaded it to 000webhost most of the time it says invalid file and only sometimes will the images successfully upload. I've tried turning up the max execution time in the php configuration but that doesn't seem to have fixed it. The files i've tried to upload are smaller than the max file size in the php config and have worked on my test machine perfectly.

我感到奇怪的是,它有时可以工作,而在其他时间不工作,也不知道该怎么做.

I find it odd that it works sometimes and doesn't other times and don't really know what to try.

这是表格 文档名称:

Here is the form Filename:

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")))
   {
       if ($_FILES["file"]["error"] > 0)
          {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
          }
       else
          {
          echo "Upload: " . $_FILES["file"]["name"] . "<br />";
          echo "Type: " . $_FILES["file"]["type"] . "<br />";
          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
          echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";


          move_uploaded_file($_FILES["file"]["tmp_name"],
          "churchimages/pauls.jpeg");
          echo "Stored in: " . "churchimages/pauls.jpeg";

          }
  }

 else

 {
 echo "Invalid file";//This is the section I am seeing
 }

推荐答案

您的文件类型检测依赖于$_FILES["file"]["type"],它是由浏览器发送的,非常不可靠.

Your file type detection is relying on $_FILES["file"]["type"], which is sent by the browser and highly unreliable.

一种更好的检测上传的文件是否为图像的方法是 getimagesize() .

A much better way to detect whether an uploaded file is an image is getimagesize().

$info = getimagesize($_FILES["file"]["tmp_name"]);

$allowed_types = array(IMG_GIF, IMG_JPEG, IMG_PNG);

if (in_array($info[2], $allowed_types))
 { .... do stuff ... }

这篇关于PHP文件上传中的文件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆