Search code examples
phphtmluploadmove

Moving a file with html and php not working


'm trying to upload a file(actually move it from a directory to another) using PHP. The problem is, every time i load the html, after i select the file to upload and press submit, it redirects me to a page that actually saves my .php file instead of the file i'm trying to move. Do i need to give permissions to a program? I am working on ubuntu 14.04.

<html>
<body>

      <form action="upload_file.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 this is the PHP script:

<?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("upload/" . $_FILES["file"]["name"])) {
                     echo $_FILES["file"]["name"] . " already exists. ";
                  } else {

                       move_uploaded_file($_FILES["file"]["tmp_name"],
                       "/home/laurentiu/Desktop/asd/" . $_FILES["file"]["name"]);
                       echo "Stored in: " . "/home/laurentiu/Desktop/asd/".
                             $_FILES["file"]["name"];
                  }
              }
} else {
    echo "Invalid file";
}
?>

Okay, now I've moved on a server.It prints:

Upload: launcher_arrow_ttb.png

Type: image/png

Size: 0.3017578125 kB

Temp file: /tmp/phpRanbFV

Stored in: /home/laurentiu/Desktop/asd/launcher_arrow_ttb.png

but nothing is uploaded. Moreover, when i var_dump the function it prints FALSE, so the function move_uploaded_file does not execute. Why i do not know...


Solution

  • This means your web server is not configured to run PHP files. Instead of executing PHP instruction in the file, it sends back the file like it would do with a plain text file.