用php上传图片,有些图片不上传

时间:2013-08-22 13:44:43

标签: php image file-upload

我正在实现一个使用PHP上传/保存图像到服务器的代码。

有些图片上传并保存,很好。这些图片是我从互联网上下载的照片或其他人给我的照片。

但是我用自己的数码相机拍摄的照片没有上传,我收到“文件无效”。现在,我的照片是.JPG。我将代码更改为还包含JPG字符串。我检查并修改了upload_max_filesize上的php.ini,因此不是大小。

我使用PHP 5.3.13

有什么想法吗?这是什么样的巫术?

修改

也许是我图片的元数据?数码相机的设置?有解决方法吗?

提前致谢

这是服务器端的代码

$allowedExts = array("gif", "jpeg", "jpg", "png");
 $temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/x-png")
 || ($_FILES["file"]["type"] == "image/png"))
 && ($_FILES["file"]["size"] < 4000000)
 && in_array($extension, $allowedExts))
   {
   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>";

     if (file_exists("cultmapsite/upload" . $_FILES["file"]["name"]))
       {
       echo $_FILES["file"]["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($_FILES["file"]["tmp_name"],
       "upload/" . $_FILES["file"]["name"]);
       echo "Stored in: " . "cultmapsite/upload" . $_FILES["file"]["name"];
       }
     }
   }
 else
   {
   echo "Invalid file";
   }

4 个答案:

答案 0 :(得分:2)

您更改了代码以包含($ _FILES [“file”] [“type”] ==“image / JPG”)但由于您还使用了explode和$ allowedExts数组,因此您还应该包含JPG。 ..

尝试

$ allowedExts = array(“gif”,“jpeg”,“jpg”,“png”,“GIF”,“JPEG”,“JPG”,“PNG”);

答案 1 :(得分:0)

尝试将数码相机中的图像转换为其他扩展名,例如.png

答案 2 :(得分:0)

尝试使用.jpg代替.JPG 我认为这可能是大写字母问题

答案 3 :(得分:0)

而不是回显&#34;无效的文件&#34;,

echo "Invalid File : " . $_FILES["file"]["name"] . $_FILES["file"]["type"];

你会明白为什么要回来。