File Upload in PHP
Advertisements
File Upload in PHP
PHP allows you to upload single and multiple files through few lines of code only. PHP file upload features allows you to upload binary and text files both. Moreover, you can have the full control over the file to be uploaded through PHP authentication and file operation functions.
First, ensure that PHP is configured to allow file uploads. In your "php.ini" file, search for the file_uploads directive, and set it to On.
Syntax
file_uploads = On
PHP $_FILES
The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.
- $_FILES['filename']['name'] It returns file name.
- $_FILES['filename']['type'] It returns MIME type of the file.
- $_FILES['filename']['size'] It returns size of the file (in bytes).
- $_FILES['filename']['tmp_name'] It returns temporary file name of the file which was stored on the server.
- $_FILES['filename']['error'] It returns error code associated with this file.
uploadform.html
<!DOCTYPE html> <html> <body> <form action="uploader.php" method="post" enctype="multipart/form-data"> Select File: <input type="file" name="fileToUpload"/> <input type="submit" value="Upload Image" name="submit"/> </form> <body> <html>
uploader.php
<?php $target_path = "c:/"; $target_path = $target_path.basename( $_FILES['fileToUpload']['name']); if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) { echo "File uploaded successfully!"; } else { echo "Sorry, file not uploaded, please try again!"; } ?>
Google Advertisment