开发者

Such a strange error while uploading a image file

开发者 https://www.devze.com 2023-01-25 18:19 出处:网络
<?php if ((($_FILES[\"file\"][\"type\"] == \"image/gif\") ($_FILES[\"file\"][\"type\"] == \"image/jpg\")
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 30000))
  {
  if ($_FILES["file"]["er开发者_StackOverflowror"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Looks Great!";

    if (file_exists("localhost/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "localhost/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "localhost/upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Something went wrong.";
  }
?>

This is the upload.php i'm using to upload an image file (jpg) to the folder called upload. I have a form elsewhere and when i choose the file and hit upload....it redirects me to upload.php and i get a message like "Something went wrong" always. I even tried png file.. Can u help?


Your code is pretty much the same as from W3Schools (http://www.w3schools.com/php/php_file_upload.asp) so I would say go back to that site and grab the code again. Also, if you used a PNG fine it would never pass your initial validation since it is not represented in your initial if..else statement.

What I would do here is step through the code and debug:

  • Does the form contain the proper file upload element with a name of 'file' on the HTML page?
  • Is the file's size less than 30kb?
  • Is the image type a gif or jpg?
  • Is the image making its way into the $_FILES object? Try doing a print_r on $_FILES to be sure
  • Do you have permission to store the image in the server's temp directory?

Good luck with tracking down the issue.


My bet goes on the form missing the enctype="multipart/form-data" attribute.

A little styling tip, you could replace the

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 30000))

By

if(in_array($_FILES["file"]["type"], 
            array("image/gif", 
                  "image/jpg", 
                  "image/pjpeg")) && 
   $_FILES["file"]["size"] < 30000)
0

精彩评论

暂无评论...
验证码 换一张
取 消