PHP File Uploading
With PHP, It is easy to upload the files on the server. For uploading the file on the server we have to use the HTML and PHP.
Using HTML we can provide the user to file upload control, where user will select the file to upload.
PHP provides the move_uploaded_file method to upload the file on the server.
Click here to check the PHP file uploading with the validation
PHP code to upload the file on the server
HTML Code:
<form method="post" enctype="multipart/form-data" name="formUploadFile"> <label>Select single file to upload:</label> <input type="file" name="file"/> <input type="submit" value="Upload File" name="btnSubmit"/> </form>
PHP Code:
if(isset($_POST["btnSubmit"])) { if($_FILES["file"]["error"] == UPLOAD_ERR_NO_FILE){ echo "Please, Select the file to upload!!!"; } else{ move_uploaded_file($_FILES["file"]["tmp_name"], "UploadFolder/".$_FILES["file"]["name"]); echo "File uploaded successfully!!!"; } }
Complete code:
<html> <head> <title>PHP upload file demo</title> </head> <body> <form method="post" enctype="multipart/form-data" name="formUploadFile"> <label>Select single file to upload:</label> <input type="file" name="file"/> <input type="submit" value="Upload File" name="btnSubmit"/> </form> <?php if(isset($_POST["btnSubmit"])) { if($_FILES["file"]["error"] == UPLOAD_ERR_NO_FILE){ echo "Please, Select the file to upload!!!"; } else{ move_uploaded_file($_FILES["file"]["tmp_name"], "UploadFolder/".$_FILES["file"]["name"]); echo "File uploaded successfully!!!"; } } ?> </body> </html>
Click here to download the PHP File Uploading Complete Source Code
Note: Before running above file uploading example on your local PHP server, first create the one folder with the named UploadFolder at the same location where the above script is saved or running.
File uploaded by you with the above example will be uploaded or save to the UploadFolder folder.
Let’s start with the HTML
In HTML we have to provide the form to user with the file upload control and the submit button, So after selecting the file when user click on the button so it will upload to the server.
Points to remember:
- HTML form must be have the “method” attribute with the “post” value. Because the file will only send to the server when the value of the method attribute of the form is post.
- HTML form must be have the “enctype” attribute with the value “multipart/form-data”. enctype attribute indicates the form have the data or value like file or binary data. enctype attribute with value multipart/form-data is required to send the file to the server.
- File upload control: We have to provide the input control with the type “file”, which allow user to select the file to upload.
PHP: move_uploaded_file() function
At the server side, In PHP we have to use the move_uploaded_file() function to accept the file coming from the client side.
move_uploaded_file() will upload the file at the specific destination or path.
Syntax:
move_uploaded_file("from/file/path", "to/file/path");
In 1st argument we have to pass the path from where we have to upload the file.
In 2nd argument we have to pass the path to where we have to the upload file or save the file.
PHP: $_FILES[]
$_FILES is the global array, Which have the all information related to the file submitted by the user or uploaded by the user.
So we also have to use the $_FILES array to upload the file.
We can use the $_FILES array in the following way:
$_FILES['file_uploader_controller_name']['property_name'];
$_FILES[] array have the following information:
$_FILES[‘file’][‘tmp_name’]: it have the path of the file. (PHP first upload the file on the some temp directory on the web server and store the path to the this variable)
$_FILES[‘file’][‘name’]: it have the real or actual name of the file.
$_FILES[‘file’][‘size’]: it have the size of the file.
$_FILES[‘file’][‘type’]: it have the MIME type of the file.
$_FILES[‘file’][‘error’]: it have the error code associated with the file.
$_FILES[‘file’][‘error’] may be have the value from the below option:
UPLOAD_ERR_OK (value: 0): It means no error, file uploaded successfully.
UPLOAD_ERR_INI_SIZE (value: 1): It means size of file is exceeds the limit which defined in the 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 at the web server.
UPLOAD_ERR_CANT_WRITE (value: 7): Failed to write file to disk.
UPLOAD_ERR_EXTENSION (value: 8): A PHP extension stopped the file upload.
Click here to download the PHP File Uploading Complete Source Code
Click here to check the PHP file uploading with the validation