1

Trying to get file uploads working on my server. File upload keeps failing. what I have done so far:
I specified the temp directory (/var/tmp/aptemp.) I assigned ownership to www-data so web server can write to it. I also set this in php.ini and checked it in phpinfo(). So far so good (I think).

I see the file in $_FILES after form submission, but the file does not make it to the /uploads folder.

Here are the permissions for the folder:

drwxr-xr-x  2 www-data       forexamplejohn 4096 Dec 16 10:52 aptemp

here is the upload script:

    <?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo " Sorry, there was an error uploading your file.";
    }
}
var_dump($_FILES); 
?>

here is the form:

    <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

here is the output after a failed attempt:

    File is an image - image/png. Sorry, there was an error uploading your file.
array (size=1)
  'fileToUpload' => 
    array (size=5)
      'name' => string '1ERJFa2kBsjhxssQb6gdhu9AtwYQwA7Xhd.png' (length=38)
      'type' => string 'image/png' (length=9)
      'tmp_name' => string '/var/tmp/aptemp/phpf2NewO' (length=25)
      'error' => int 0
      'size' => int 11063
user3738926
  • 948
  • 1
  • 7
  • 15
  • 1
    http://stackoverflow.com/a/13841017/1415724 *"the temporary file; PHP cleans up after itself after a successful upload"*. If temp files keep stacking up, then that would mean the upload failed. – Funk Forty Niner Dec 16 '16 at 19:15
  • ok, but should I not see the temp file just prior to form submission? – user3738926 Dec 16 '16 at 19:20
  • that would be rather hard to keep track. – Funk Forty Niner Dec 16 '16 at 19:20
  • I would just browse to the folder and have a peek no? – user3738926 Dec 16 '16 at 19:21
  • if it's a big file and you have time to peek at it during that time it takes, sure... I'd say it would probably be visible. – Funk Forty Niner Dec 16 '16 at 19:21
  • in the form I browse for a file and specify said file (no submission.) At this point, the file should be visible in the temp directory. Once the form is submitted, and file uploaded, then cleanup should take place. The problem is, I do not see said file before form submission. – user3738926 Dec 16 '16 at 19:25
  • no; when you choose the file and didn't upload it yet, it won't be visible. It's only when it starts to upload from your computer to the server. – Funk Forty Niner Dec 16 '16 at 19:26
  • I see. Thanks for clarifying. – user3738926 Dec 16 '16 at 19:27
  • you're welcome. I believe the link I included at first should be used to mark it off as a duplicate; do you agree? or would you rather I use my comments as an answer? I don't mind if the duplicate. am not in this for the rep. – Funk Forty Niner Dec 16 '16 at 19:27
  • I am trying to troubleshoot an issue. The link you posted doesn't get me to a solution, and I don't believe the questions are substantially similar. – user3738926 Dec 16 '16 at 19:31
  • tell you what; use a different folder name, one being non-existant and you'll see that temp file in that folder; am next to certain about this. That or change permissions to the folder. However, you say "troubleshoot", troubleshoot what exactly? I feel like I may be missing something here, or I have misread your question. – Funk Forty Niner Dec 16 '16 at 19:34
  • I created this folder for this purpose. It was not pre-existing. I am trying to get the file upload to work regardless of what the temp folder is doing. I re-wrote the question. – user3738926 Dec 16 '16 at 19:37
  • someone gave you an answer below; ask them. I've done what I could here. – Funk Forty Niner Dec 16 '16 at 19:37
  • they must have removed it. I don't see anything. – user3738926 Dec 16 '16 at 19:42
  • last ditch effort: try a full server path to the upload folder. `$target_dir = "/var/user/public/uploads/";` as an example - Edit: no it's there alright, the answer. Reload your question. and if my comment here solves it, let me know. – Funk Forty Niner Dec 16 '16 at 19:42
  • did you not try a full server path to the upload folder? `$target_dir = "/var/user/public/uploads/";` as I already stated 10 mins. prior to this comment. – Funk Forty Niner Dec 16 '16 at 19:52
  • just now, yes. same result – user3738926 Dec 16 '16 at 19:55

1 Answers1

1

move_uploaded_file() will return false when:

a) the filename has not been uploaded via POST

b) the file could not be copied to the destination directory

It looks like b is the most likely culprit here. In that case there should also be a warning in your server logs explaining the problem. Check permissions on the destination directory, and confirm that you've configured SELinux/AppArmor to allow the web server to write to the directory.

Community
  • 1
  • 1
miken32
  • 35,483
  • 13
  • 81
  • 108
  • thefile was uploaded via post as showing in the form and output. Also, I changed permissions to 777, restarted server, and result is the same. Will check logs here shortly. – user3738926 Dec 16 '16 at 19:51
  • It was the destination folder. Changed ownership and uploads now work. lol – user3738926 Dec 16 '16 at 20:11