Rob Avery IV
Rob Avery IV

Reputation: 3730

move_uploaded_files() doesn't move file without error

I'm trying to allow my website to upload files to the server. Here is my code so far:

form_upload.html

<html>
<body>

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

file_upload.php

<?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("uploads/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      if(!move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"])){
          echo "Did not store file.";
      }else{
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      }
    }
  }
} else {
  echo "Invalid file";
}
?>

Everything works fine, but when I go to look on my server, I don't see the file in the /uploads folder. When the form is submitted, the php file echos this:

Upload: square-big-button.jpg Type: image/jpeg Size: 9.875 kB Temp file: /tmp/phpyhjzZF Stored in: uploads/square-big-button.jpg

Of course, Temp file will always be different.

What is going on? Why is it saying it was uploaded when it wasn't?

SOLUTION:

I just needed to add a ../uploads/ as the dir.

Upvotes: 1

Views: 2533

Answers (3)

Charles
Charles

Reputation: 437

Since you said your uploaded file is in the root, and you are sure of it, then this should work.

move_uploaded_file($_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/uploads/" .   $_FILES["file"]["name"]);

Just tested it out now on my localhost, and it has moved the file to my uploads directory located at the root of my web directory.

Upvotes: 2

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try to give a correct path of location where you want to save . also check dir have writable permission if you uploads dir is one up level us ../ to move up and so on

move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);

if uploads dir is on your server root try

move_uploaded_file($_FILES["file"]["tmp_name"], "./uploads/" . $_FILES["file"]["name"]);

Upvotes: 3

dinhokz
dinhokz

Reputation: 937

It's working fine here, maybe you have placed the uploads directory in the wrong place. The uploads directory should be in the same directory as the files, or you should use the right relative path as ../uploads. Can you tell us the path for each files/folder?

Also, this line seems to be wrong.

if (file_exists("upload/" . $_FILES["file"]["name"])) {

it should be verifying the uploads directory.

Upvotes: 1

Related Questions