kumiau
kumiau

Reputation: 724

File upload not working

I installed wordpress in a media temple server and noticed that couldn't upload files. After tryng several solutions decided to replicate the problem with a simple script (http://pastie.org/2349720).

<?php
if($_FILES)
        print_r($_FILES);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
  {
  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 (!file_exists($_FILES["file"]["tmp_name"]))
      {
      echo 'temp file doesn\'t exists';
      }
    else
      {
      $uploaded=move_uploaded_file($_FILES["file"]["tmp_name"],
      $_FILES["file"]["name"]);
      if($uploaded)
        echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
      else
        echo "fallo la carga del archivo";
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

<html>
<body>

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

Looks like move_upload_file() is returning false without a warning or error, looked in php documentation and found this

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

I've checked and the file upload doesn't report any errors and the temporary file exist before trying to move it. Already set 777 permission in folder and still not having any luck, any idea on what can be causing this behavior and how to fix it?

thanks.

Upvotes: 3

Views: 1554

Answers (7)

magorich
magorich

Reputation: 459

I don't know if this helps but I found this problem a few hours ago and the problem was the write permissions of my folder (I don't own the server I was working on) and the response for the people I work with was

The "yours" folder must be writable by the user apache runs as (www-data) this can be accomplished by making the folder world writable or setting the "where you want to put your files" folder to be owned by group www-data and make it group writable.

Upvotes: 0

bolaji
bolaji

Reputation: 127

check if u can reach d dir, There is no need in deleting it in temporary folder since it will be moved automatically,the target destination is still within d file. so why dont you try moving it to another destination

Upvotes: 0

kumiau
kumiau

Reputation: 724

Turns out i had fastCGI disabled, i've just enabled and things are working as expected. Thanks for your help.

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146588

You are checking whether destination exists in certain directory and then copying source to another directory:

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

Upvotes: 1

judda
judda

Reputation: 3972

move_uploaded_file will also fail if the destination is not writable. Is the root folder writable (I ask that because you aren't tacking on the uploads folder into the filename when moving it). I know you mentioned you set the permission to 777 but is that for the uploads directory or the entire folder that the application is in.

Upvotes: 1

cutsoy
cutsoy

Reputation: 10251

What does this give you:

$_FILES[...]['error']

If it doesn't give you zero, you can check what goes wrong here: http://php.net/manual/en/features.file-upload.errors.php.

If it does, make sure the folder your folder in is accessible by PHP. You could try running this command:

sudo -s -u [php_user]
cd [destination_directory]

Check whether you can reach the directory =)!

Upvotes: 1

Savageman
Savageman

Reputation: 9487

Try absolute paths for the destination of move_uploaded_file()

Upvotes: 0

Related Questions