Upload File in PHP
A PHP script can be used with an HTML form that allows users to upload files to the Server. First these files are uploaded to a temporary directory, then moved to a destination by a PHP script.
The information in the phpinfo.php page describes the temporary directory that is used for files uploaded in upload_tmp_dir and the limited size of the files that can be uploaded start in upload_max_filesize format. These parameters are set in the php.ini configuration file.
The process of uploading a file follows these steps:
- The user opens the page containing an HTML form as a text file, a browse button and a submit button.
- Users click browse and select a file to upload from the local computer.
- The full path to the selected file will appear in the text field, then the user clicks the Submit button.
- The selected file is sent to the temporary folder on the Server.
- PHP script, which is defined as a Form Handler in the form's action attribute, will check if the file has arrived and then copy this file to the desired directory.
- PHP script successfully confirms to the user.
Normally when writing files, it is necessary for both temporary folders and the destination directory to have access permissions set to write permissions. If either is set to read-only, the process will fail.
A file uploaded can be a text file, an image file or any document.
Create an upload form in PHP
Below is the HTML code that creates an upload form. This form has the method attribute set to post and the enctype attribute set to multipart / form-data .
php if ( isset ( $_FILES [ 'image' ])){ $errors = array (); $file_name = $_FILES [ 'image' ][ 'name' ]; $file_size = $_FILES [ 'image' ][ 'size' ]; $file_tmp = $_FILES [ 'image' ][ 'tmp_name' ]; $file_type = $_FILES [ 'image' ][ 'type' ]; $file_ext = strtolower ( end ( explode ( '.' , $_FILES [ 'image' ][ 'name' ]))); $expensions = array ( "jpeg" , "jpg" , "png" ); if ( in_array ( $file_ext , $expensions )=== false ){ $errors []= "Định dạng không được hỗ trợ, hãy chọn ảnh JPEG hoặc PNG." ; } if ( $file_size > 2097152 ){ $errors []= 'File phải có dung lượng tối thiểu 2 MB' ; } if ( empty ( $errors )== true ){ move_uploaded_file ( $file_tmp , "images/" . $file_name ); echo "Thành công!" ; } else { print_r ( $errors ); } } ?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Create upload script in PHP
There is a global PHP variable of $ _FILES. This variable is an array of conjugated arrays and holds all information related to the uploaded file. So, if the value assigned to the name attribute of the input in the upload form is a file, then PHP can create the following 5 variables:
- $ _FILES ['file'] ['tmp_name'] - File uploaded in a temporary folder on the Web Server.
- $ _FILES ['file'] ['name'] - The actual name of the uploaded file.
- $ _FILES ['file'] ['size'] - The size in bytes of the uploaded file.
- $ _FILES ['file'] ['type'] - MIME type of uploaded file.
- $ _FILES ['file'] ['error'] - Encrypt the error associated with this upload file.
For example
Below is an example to allow loading images and returning results in the form of uploaded file information.
php if ( isset ( $_FILES [ 'image' ])){ $errors = array (); $file_name = $_FILES [ 'image' ][ 'name' ]; $file_size = $_FILES [ 'image' ][ 'size' ]; $file_tmp = $_FILES [ 'image' ][ 'tmp_name' ]; $file_type = $_FILES [ 'image' ][ 'type' ]; $file_ext = strtolower ( end ( explode ( '.' , $_FILES [ 'image' ][ 'name' ]))); $expensions = array ( "jpeg" , "jpg" , "png" ); if ( in_array ( $file_ext , $expensions )=== false ){ $errors []= "Định dạng không được hỗ trợ, hãy chọn ảnh JPEG hoặc PNG." ; } if ( $file_size > 2097152 ) { $errors []= 'File phải có dung lượng tối thiểu 2 MB' ; } if ( empty ( $errors )== true ) { move_uploaded_file ( $file_tmp , "images/" . $file_name ); echo "Thành công!" ; } else { print_r ( $errors ); } } ?>
Follow tutorialspoint
Previous article: Send Email using PHP
Next lesson: Standard writing code in PHP
You should read it
- Form - Form in CSS
- How to Create a Form in a Spreadsheet
- Create upload file with VB.NET
- How to create a simple contact form using HTML, CSS, and JavaScript
- MS Access 2007 - Lesson 12: Design Forms
- How to create a professional Google Form
- How to create a website contact form with Google Forms
- How to insert a poll from Google Form into an email
May be interested
- Guide to upload HD videos to Facebook with iPhoneuploading videos to facebook is too simple, but to upload hd videos to facebook you will need some small settings. see how to upload hd videos to facebook in this article to know how to do them.
- Why is upload speed usually much slower than download speed?if you regularly check the internet connection speed at home or office, it will not be difficult for you to realize that the download speed is usually much faster than the upload speed.
- How to update files on Google Drive does not change the shared linkon google drive, if you upload any file and share the file link for other users, when you change the file, you will have to re-share the updated (updated) file again. in addition google drive also allows you to upload multiple files with the same name and format, which means you will not be able to replace old files that have been uploaded if the new file has the same name.
- What is the difference between upload and download speed?if you've ever conducted an internet speed test, chances are you've come across two distinct values: upload and download speeds.
- What is the Microsoft Office Upload Center? How to disable this tool?after you install microsoft office on windows, you may see a small orange icon in the notification area. this icon is the microsoft office upload center feature, but if you do not use this feature, you can disable it to free memory.
- Upload all content from any DVD to YouTubein the following article, tipsmake.com will introduce you to the basic steps to upload all content inside any dvd video disc to youtube ...
- General tools Upload and share data right on the Desktopthis article will synthesize a number of applications to upload and share data right on the desktop screen.
- Allow others to upload data to your Google Drivea script can help anyone can upload data to your google drive. google drive is a very popular online storage service, but sharing data between users must often be via a shared link created from the application.
- How to Email Audio Filesthis wikihow teaches you how to send an audio file via email. in cases involving large audio files, you'll first need to upload the file to a cloud service (e.g., google drive) and then share it from there. locate your audio file. you can...
- How to have friends upload videos to your YouTubeyoutube uploader will help others post videos to your youtube channel without an account.