skip to Main Content

So I’m working on a website for my mom where she wants to upload photo’s from her camera, I first tested my code with a normal picture that I found on my computer that was created in photoshop CS6 and the $_FILES array was okay:

array ( 'img' => array ( 'name' => array ( 0 => 'pic01.jpg', ), 'type' => array ( 0 => 'image/jpeg', ), 'tmp_name' => array ( 0 => 'D:\xampp\tmp\phpF0F7.tmp', ), 'error' => array ( 0 => 0, ), 'size' => array ( 0 => 6311, ), ), )

but when I try to upload a photo of my phone and hers I get the following array:

array ( 'img' => array ( 'name' => array ( 0 => 'IMG_20180228_143837.jpg', ), 'type' => array ( 0 => '', ), 'tmp_name' => array ( 0 => '', ), 'error' => array ( 0 => 1, ), 'size' => array ( 0 => 0, ), ), )

as you can see the type, tmp_name and size are empty or incorrect(the size is incorrect). I also see that the error array value is changed from 0 to 1.

both of these array exports are made by var_export($_FILES);

I use the following HTML code to upload the image:

<form method="post" action="updateproduct.php" enctype="multipart/form-data">{
      <input name="img[]" id="fileupload" type="file" multiple />

I hope I have given enough information if not please say so.

2

Answers


  1. An error value of 1 in your $_FILES array means you’ve exceeded the file size limit imposed by the server:

    UPLOAD_ERR_INI_SIZE

    Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

    Source

    Login or Signup to reply.
  2. By default PHP accepts file uploads just under 2MB. Consider changing it to something more realistic (like 200MB) in your PHP config (php.ini). Also do not forget to increase another relevant option post_max_size, which should be somewhat larger than upload_max_filesize.

    Btw, here’s a nice helper function that will translate encoded error to the human readable message:

        function codeToMessage($code) 
        { 
            switch ($code) { 
                case UPLOAD_ERR_INI_SIZE: 
                    $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; 
                    break; 
                case UPLOAD_ERR_FORM_SIZE: 
                    $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
                    break; 
                case UPLOAD_ERR_PARTIAL: 
                    $message = "The uploaded file was only partially uploaded"; 
                    break; 
                case UPLOAD_ERR_NO_FILE: 
                    $message = "No file was uploaded"; 
                    break; 
                case UPLOAD_ERR_NO_TMP_DIR: 
                    $message = "Missing a temporary folder"; 
                    break; 
                case UPLOAD_ERR_CANT_WRITE: 
                    $message = "Failed to write file to disk"; 
                    break; 
                case UPLOAD_ERR_EXTENSION: 
                    $message = "File upload stopped by extension"; 
                    break; 
    
                default: 
                    $message = "Unknown upload error"; 
                    break; 
            } 
            return $message; 
        } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search