Created
March 29, 2021 15:35
-
-
Save farithadnan/a94d75ae9d4b4f658b233bf2738167c7 to your computer and use it in GitHub Desktop.
Basic uploading file with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if (isset($_POST['submit'])) { | |
| // Displaying the array detail of that files | |
| // echo "<pre>"; | |
| // print_r($_FILES['file_upload']); | |
| // echo "</pre>"; | |
| // ERROR INFORMATION | |
| // UPLOAD_ERR_OK | |
| // Value: 0; There is no error, the file uploaded with success. | |
| // UPLOAD_ERR_INI_SIZE | |
| // Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. | |
| // UPLOAD_ERR_FORM_SIZE | |
| // Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. | |
| // UPLOAD_ERR_PARTIAL | |
| // Value: 3; The uploaded file was only partially uploaded. | |
| // UPLOAD_ERR_NO_FILE | |
| // Value: 4; No file was uploaded. | |
| // UPLOAD_ERR_NO_TMP_DIR | |
| // Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3. | |
| // UPLOAD_ERR_CANT_WRITE | |
| // Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. | |
| // UPLOAD_ERR_EXTENSION | |
| // Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0. | |
| // | |
| // Upload built in error | |
| $upload_errors = array( | |
| 0 => 'There is no error, the file uploaded with success', | |
| 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', | |
| 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', | |
| 3 => 'The uploaded file was only partially uploaded', | |
| 4 => 'No file was uploaded', | |
| 6 => 'Missing a temporary folder', | |
| 7 => 'Failed to write file to disk.', | |
| 8 => 'A PHP extension stopped the file upload.', | |
| ); | |
| // Set up for moving the temp file to permanent location | |
| $temp_name = $_FILES['file_upload']['tmp_name']; | |
| $the_file = $_FILES['file_upload']['name']; | |
| // Make sure you have created this file in the directory, same as upload.php location | |
| $directory = "uploads"; | |
| // Moving file execution if succesfull it will print success message | |
| if (move_uploaded_file($temp_name, $directory . "/" . $the_file)) { | |
| $the_message = "File Uploaded Succesfully!"; | |
| } else { | |
| // if not succesful it will print out an erro | |
| $the_error = $_FILES['file_upload']['error']; | |
| $the_message = $upload_errors[$the_error]; | |
| } | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>File uploads</title> | |
| </head> | |
| <body> | |
| <!-- if need to send various different type of data we need to include enctype data --> | |
| <form action="upload.php" enctype="multipart/form-data" method="post"> | |
| <h2> | |
| <?php | |
| if(!empty($upload_errors)){ | |
| echo $the_message; | |
| } | |
| ?> | |
| </h2> | |
| <input type="file" name="file_upload"> | |
| <br/> | |
| <input type="submit" name="submit"> | |
| </form> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment