Uploading files to the server

PHP
29.05.19

Consider uploading files to a PHP server using the POST method. This will require a form with the file field type and an enctype attribute with a multipart/form-data value.

HTML

<form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    <input type="file" name="uploaded_file">
    <input type="submit" name="submit" value="send">
</form>

The field with name="MAX_FILE_SIZE" must be placed above the input type="file", the value is set in bytes. The field is optional and the check must be still on the server. After sending the data of the uploaded file get into the $_FILES array

PHP

if(isset($_FILES["uploaded_file"])){ //field´s name type="file"
    $maxsize = 1024 * 1024* 2; //limited the allowed file size to 2 MB
    $errors     = array();
    $acceptable = array( //valid file extensions
        'application/pdf',
        'image/jpeg',
        'image/jpg',
        'image/gif',
        'image/png'
    );
 
    if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
        if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
            $errors[] = 'File too large. File must be less than 2 megabytes.';
        }
        if(!in_array($_FILES['uploaded_file']['type'], $acceptable) && (!empty($_FILES["uploaded_file"]["type"]))){
            $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
        }
        if(!count($errors)) {
            $uploaddir = __DIR__ . '/upload/'; //upload folder
            $uploadfile = $uploaddir . $_FILES['uploaded_file']['name'];
            move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadfile);
        } else {
            foreach($errors as $error) {
                echo $error . '<br />';
            }
        }
    }
}

You can upload multiple files at once. PHP supports the ability to transfer an array of HTML including files.

HTML

<form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="submit" name="submit" value="send">
</form>

PHP

if(isset($_FILES["pictures"])){
   $maxsize = 1024 * 1024* 2;
   $errors     = array();
   $acceptable = array(
       'application/pdf',
       'image/jpeg',
       'image/jpg',
       'image/gif',
       'image/png'
   );
 
   foreach ($_FILES["pictures"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
             if (is_uploaded_file($_FILES['pictures']['tmp_name'][$key])) {
               if(($_FILES['pictures']['size'][$key] >= $maxsize) || ($_FILES["pictures"]["size"][$key] == 0)) {
                   $errors[] = 'File too large. File must be less than 2 megabytes.';
               }
               if(!in_array($_FILES['pictures']['type'][$key], $acceptable) && (!empty($_FILES["pictures"]["type"][$key]))){
                   $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
               }
               if(!count($errors)) {
                   $uploaddir = __DIR__ . '/upload/'; 
                   $uploadfile = $uploaddir . $_FILES['pictures']['name'][$key];
                   move_uploaded_file($_FILES['pictures']['tmp_name'][$key], $uploadfile);
               } else {
                   foreach($errors as $error) {
                       echo $error . '<br />';
                   }
               }
            
            }
        }
    }
}
blog comments powered by Disqus