User uploaded image

I have form and one of the fields look like this…

<label for="file">Upload image</label>
<input type="file" id="file" name="userimage" accept="image/gif, image/jpeg, image/png, image/jpeg, image/gif" required /> 

When I try to read the size of this image on the server with this code…

error_log($_FILES['userimage']['size']);


I receive this notice in the error log..

[client ::1:32866] PHP Notice:  Undefined index: userimage

What am I doing wrong?

Undefined index typically is one or the other

  • You are referencing a variable that was not assigned/created.
  • You are referencing an index of an array that was does not exist.

From what I can only speculate, the image isn’t being uploaded. Assuming that you didn’t check to make sure an image was actually uploaded and is moved to the /tmp directory.

Well I placed this code inside…

 if ($_SERVER['REQUEST_METHOD'] == 'POST') {

      error_log($_FILES['userimage']['size']); 
      /* I am getting all my other variables here no problem. */

}

How would I check that?

The very first thing before trying to use the error_log function is to do a print_r($_FILES)
This way, I can see what is going through to the $_FILES variable. If the name or any of the indexes are empty, that means that those images some how aren’t being sent to the /tmp directory. In which case, that also means that there’s something wrong with your server configurations or Apache/ Nginx doesn’t have permissions to move temporary files to /tmp.

output of above is empty

Then I’d like to see the form part along with the entire file for this section.

I looked at my php_info…

upload_max_filesize	2M	2M
upload_tmp_dir	no value	no value
user_dir	no value	no value

Oh right. The upload size could be the issue. The tmp folder doesn’t necessarily need to be modified, but making sure that Apache or Nginx can reach it.

Change the upload_max_filesize to 128M and the post_max_size to 128M as well.

Is 2M actually 2 mb?
Image I was trying to upload is 55kb

Yes. It does mean megabytes. Try it and see if anything changes.

No.Still same. When I run this code

 if (empty($_POST["my_image"])) {  /* If input is empty .*/
           $imageErr = "Image is required."; /* Set error message.*/
           $ok = false;  /* Data not ready. */
  } 

I get “Image is required” printed on the screen

That’s because uploading files should only use $_FILES not $_POST.

stil comes out empty

Then read post #7.

@myLinux:/$ ls -l /tmp
total 20
-rw------- 1     0 Mar 11 18:24 config-err-vbDkCO
drwx------ 3 root root 4096 Mar 11 18:24 systemd-private-803f379f76f64dcfaafa73aae3ad4a10-colord.service-VTF0fT
drwx------ 3 root root 4096 Mar 11 18:25 systemd-private-803f379f76f64dcfaafa73aae3ad4a10-fwupd.service-30f5nE
drwx------ 3 root root 4096 Mar 11 18:23 systemd-private-803f379f76f64dcfaafa73aae3ad4a10-rtkit-daemon.service-a7XBlU
drwx------ 3 root root 4096 Mar 11 18:22 systemd-private-803f379f76f64dcfaafa73aae3ad4a10-systemd-timesyncd.service-0iShX3
drwx------ 2  4096 Mar 11 18:25 tracker-extract-files.1000

Please provide the HTML and PHP codes so I can determine what is going on.

upload.php (1.4 KB)
test.html (280 Bytes)

Please post them on the forums. I am not going to download those files. Use the </> icon to wrap your codes.


<?php
$target_dir = "/img/user_upl/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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.";
    }
}
?>


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

Code from above gives me this error in apache log…

[Sun Mar 11 23:37:05.465389 2018] [:error] [pid 7769] [client ::1:35902] PHP Warning:  move_uploaded_file(/img/user_upl/home_icon_android.png): failed to open stream: No such file or directory in /var/www/part/upload.php on line 39, referer: http://localhost/part/test.html
[Sun Mar 11 23:37:05.465432 2018] [:error] [pid 7769] [client ::1:35902] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpMkUHHd' to '/img/user_upl/home_icon_android.png' in /var/www/part/upload.php on line 39, referer: http://localhost/part/test.html