7

I copied the code from this page: http://www.w3schools.com/php/php_file_upload.asp

Problem is that i don't get an error when uploading a file, instead this shows which should be correct:

Upload: images.jpeg Type: image/jpeg Size: 5.8603515625 kB Temp file: /tmp/phpZ67YXk Stored in: upload/images.jpeg

But no file is saved on the server.

I don't know what is wrong but I'm thinking in terms of permission, still there is a folder named upload with 777 permissions.

These php-files are hosted on a online web host so I don't run this locally.

HTML-form

<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>

upload_file.php

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
4
  • 1
    $success = move_uploaded_file(/*...*/) -- check the return status of move_uploaded_file. It returns true on success. Looks like it should also be raising a PHP warning, if there is in fact an error. Check your error logs. Commented Jan 22, 2013 at 0:42
  • You're right, move_uploaded_file() fails. In another post they suggested that this may be because the folder doesn't have write permissions for world. Is this something I can change or do I have to ask my web host to do that? Commented Jan 22, 2013 at 1:06
  • you told us 777 permission means max permissions (including world write) Commented Jan 22, 2013 at 1:09
  • Alright! Did not know that :) Commented Jan 23, 2013 at 22:28

2 Answers 2

8

I suppose the problem is in your relative path of destination (relative according to your current working directory = path of your .php file), try to make it absolutem like this:

move_uploaded_file(
  $_FILES["file"]["tmp_name"],
  $_SERVER['DOCUMENT_ROOT'] . 'upload/' . $_FILES["file"]["name"]
);
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget set 777 permission for your upload/ directory. For ubuntu $sudo chmod -R 777 uploads/
1

this help you:

chmod 777 foldername

php code:

<?php
if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
  $expensions= array("jpeg","jpg","png", "mp3",
                     "acc", "wav", "3gpp", "mp4", "3gp", "m4a", "amr", "avi",
                    "flv", "gif");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152666655){
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"/var/www/upload/users/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
}
?>

html:

<html>
<body>

  <form action="" method="POST" enctype="multipart/form-data">
     <input type="file" name="image" />
     <input type="submit"/>
  </form>

</body>
</html>

1 Comment

Please add a little bit of text to that code-only answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.