简体   繁体   中英

Files not being uploaded to server using PHP script

So Im following a simple tutorial/example on how to upload files to a web server using HTML and PHP. There are no errors and everything seems to work fine, but the file is not saved on the web server...

I'm using an Amazon EC2 instance. EDIT: I now have permission warnings/errors in my echo output as shown below, even after running chmod 777 on the directories

index.html

<form enctype="multipart/form-data" action="test.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

test.php

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png","tiff");
$temp = explode(".", $_FILES["uploadedfile"]["name"]);
$extension = end($temp);
echo "Upload attempt...: " . $_FILES["uploadedfile"]["name"] . "<br>";
if ((($_FILES["uploadedfile"]["type"] == "image/gif")
     || ($_FILES["uploadedfile"]["type"] == "image/jpeg")
     || ($_FILES["uploadedfile"]["type"] == "image/tiff")
     || ($_FILES["uploadedfile"]["type"] == "image/jpg")
     || ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
     || ($_FILES["uploadedfile"]["type"] == "image/x-png")
     || ($_FILES["uploadedfile"]["type"] == "image/png"))
    && ($_FILES["uploadedfile"]["size"] < 20000000)
    && in_array($extension, $allowedExts)) {

  if ($_FILES["uploadedfile"]["error"] > 0) {
    echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br>";
    echo "Type: " . $_FILES["uploadedfile"]["type"] . "<br>";
    echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["uploadedfile"]["tmp_name"] . "<br>";
    if (file_exists("~/upload/" . $_FILES["uploadedfile"]["name"])) {
      echo $_FILES["uploadedfile"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
                         "../upload/" . $_FILES["uploadedfile"]["name"]);
      echo "Stored in: " . "../upload/" . $_FILES["uploadedfile"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>

echo output

Upload attempt...: storeflyer.jpg
Upload: storeflyer.jpg
Type: image/jpeg
Size: 35.1708984375 kB
Temp file: /tmp/phpqq6J4C
Warning: move_uploaded_file(../upload/storeflyer.jpg): failed to open stream: Permission denied in ../test.php on line 29 Warning: move_uploaded_file(): Unable to move '/tmp/phpqq6J4C' to '../upload/storeflyer.jpg' in ../test.php on line 29 Stored in: ../upload/storeflyer.jpg

You're using ~/upload/ with a tilde and slash as path.

You should remove all of the tildes and slashes by using upload/ as a relative path.

Or, as stated by sonic720 in a comment: /var/www/upload this will differ from server to server, so yours may be something like /var/users/www/public_html/upload that is another option.

Use upload/ if executing your script from the root of your server.

If executed from a sub-folder, you may need to adjust it to, and for example: ../upload/

Also check for folder permissions. Either 0755 or 0777 , yet 0755 is safer to use.

  • About: __DIR__ & BASE_PATH

Consult: http://php.net/manual/en/function.dirname.php


Edit:

I noticed something else echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; that should read as echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>"; echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br>";


Add error reporting to the top of your file(s) which will help during production testing.

error_reporting(E_ALL);
ini_set('display_errors', 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM