简体   繁体   English

无法在服务器中上传JPEG文件

[英]Unable to upload JPEG file in the server

I am trying to upload images in my PHP server using HTML form. 我正在尝试使用HTML表单在PHP服务器中上传图像。 I have written the following code. 我写了下面的代码。 Its not creating problem when i upload a 'png' file, but when i upload 'JPEG' images it says 'Invalid File'. 当我上传一个“ png”文件时,它不会产生问题,但是当我上传“ JPEG”图像时,它显示“无效文件”。 Kindly check it and guid me. 请检查并引导我。

Thanks 谢谢

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="file_upload_test2.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



</body>
</html>


<?php

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
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/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& 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"] / 200000) . " kB<br>";
   // echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("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: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }


?> 

If you host your application on linux, you should convert extension to lower case to match strings in $allowedExts , because linux is case sensitive (and not just linux). 如果将应用程序托管在linux上,则应将扩展名转换为小写形式以匹配$allowedExts中的字符串,因为linux区分大小写(而不仅仅是linux)。

Check this How to track execution time of each line / block of lines / methods in PHP? 选中此复选框如何跟踪PHP中每行/每行/每个方法的执行时间? to be able to run script step by step and see, where the problem is. 以便能够逐步运行脚本并查看问题出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM