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 ); } } ?>
You should read it
- How to create a Google Form form on Google Drive
- HTML form
- 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
- MS Access 2003 - Lesson 35: Creating a Form using the Form Wizards
- How to Design a Form
Maybe you are interested
How to clean junk files when drive C on the computer is full
Crack Excel password, remove Sheet password in Excel file
How to cut MP3 music without using software, export MP3 file
Gemini can now recognize PDF files appearing on smartphone screens
9 ways to fix the error of not being able to save files as JPEG or PNG in Photoshop
Download standard and beautiful Word file experience initiative cover template