PHP File upload

PHP

< html lang = "en" > < head > < meta charset = "UTF-8" > < title > File Upload Form < / title > < / head > < body > < form action = "file-upload-manager.php" method = "post" enctype = "multipart / form-data" > < h2 > Upload File < / h2 > < label for = "fileSelect" > Filename: < / label > < input type = "file" name = "photo" id = "fileSelect" > < input type = "submit" name = "submit" value = "Upload" > < p > < strong > Note: < / strong > Only .jpg, .jpeg, .png formats allowed to a max size of 2MB. < / p > < / form > < / body > < / html > Now it`s time to write a php script that can handle the boot system files. 
files for load-manager.php
// Check if the form was submitted if ( $_ SERVER [ " REQUEST_METHOD " ] == "POST" ) { // Check if the file was uploaded without errors if (isset (< / code> $_ FILES [ "photo" ]) & amp; & amp; $_ FILES [ "photo" ] [ "error" ] == 0) {< / code> $allowed_ext = array ( "jpg" = > "image / jpg" , "jpeg" = > "image / jpeg" , "gif" = > "image / gif" , "png" = > "image / png" ); $file_name = $_ FILES [ "photo" ] [ "name" ]; $file_type = $_ FILES [ "photo" ] [ "type" ]; $file_size = $_ FILES [ "photo" ] [ "size" ]; // Check file extension $ext = pathinfo ( $filename , PATHINFO_EXTENSION); if (! array_key_exists ( $ext , $allowed_ext )) die ( "Error: Please select a valid file format." ); // Check file size - less than 2 MB $maxsize = 2 * 1024 * 1024; if ( $file_size > $maxsize ) die ( " Error: File size is larger than the allowed limit. " ); // Check MYME file type if (in_array ( $file_type , $allowed_ext )) { // Check if the file exists before loading it if ( file_exists ( "upload /" . $_FILES [ "photo" ] [ " name " ])) echo $_ FILES [ " photo " ] [ "name" ]. "is already exists." ; else { move_uploaded_file ( $_ FILES [ "photo" ] [ "tmp_name" ], " uploads / " . $_ FILES [ " photo " ] [ "name" ]); echo "Your file was uploaded successfully. " ; } } else { echo " Error: Please try again. " ; } } else { echo " Error: " . $_ FILES [ "photo" ] [ "error" ]; } }
?>
In the above scenario, when we submit the form, later we we can access the information through the PHP $_FILES superglobal associative array. Besides the form using the $_FILES array, many built-in functions play an important role. After we have finished uploading the file, in the script we will check the server request method, if it is POST, then it will continue, otherwise the system will give an error.Later, we accessed the $_FILES array to get the file name, file size and file type. Once we receive this data, we will check the file size and type. At the end, we look in the folder where the file should be downloaded to check if the file already exists or not. If not, we used move_uploaded_file() to move the file from the temporary location to the correct directory on the server and you`re done. 
Exit document.createElement (`video`);



Get Solution for free from DataCamp guru