Search code examples
phphtmlimageformsimage-uploading

php script is not uploading image to specified folder


although I know that is has been everywhere as a question, I can't find the reason it doesn't work:

I have gathered the code below to achieve image uploading to my site:

<html>
<body>

<form action="insert.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>

and the insert.php is:

<?php
$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/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& 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("/httpdocs/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/httpdocs/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/httpdocs/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
     echo "Invalid file";
  }
?>

Well,the success messages are displayed correctly. However, no file is uploaded to the specified folder.

Any thoughts?


Solution

  • Run the script from your root, create a folder called uploads then change all instances of /httpdocs/ to uploads/ (as I did below) without the opening / and it should (theoretically) work, as it did for me.

    A few things to note is to make sure that this < 20000 is increased if your file is less than 20,000 bytes. If the file you attempt to use to upload is higher than this number, then the script will throw an error of Invalid file

    Make sure the folder is writeable e.g.: 0755 or 0777 (in my case it's 0755)

    This has been tested on my own server using uploads as my uploading folder.

    Plus as Nikolay states in his answer, you may want to check that also.

    <?php
    $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/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000) // <= you may have to increase this value
    && 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("uploads/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
         echo "Invalid file";
      }
    ?>