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.

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:

  1. The user opens the page containing an HTML form as a text file, a browse button and a submit button.
  2. Users click browse and select a file to upload from the local computer.
  3. The full path to the selected file will appear in the text field, then the user clicks the Submit button.
  4. The selected file is sent to the temporary folder on the Server.
  5. 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.
  6. 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 ); } } ?>  action = "" method = "POST" enctype = "multipart/form-data" >  type = "file" name = "image" />  type = "submit" /> 

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:

Picture 1 of Upload File in PHP

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:

  1. $ _FILES ['file'] ['tmp_name'] - File uploaded in a temporary folder on the Web Server.
  2. $ _FILES ['file'] ['name'] - The actual name of the uploaded file.
  3. $ _FILES ['file'] ['size'] - The size in bytes of the uploaded file.
  4. $ _FILES ['file'] ['type'] - MIME type of uploaded file.
  5. $ _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 ); } } ?>  action = "" method = "POST" enctype = "multipart/form-data" >  type = "file" name = "image" />  type = "submit" /> Sent file:  php echo $_FILES [ 'image' ][ 'name' ]; ?> File size:  php echo $_FILES [ 'image' ][ 'size' ]; ?> File type:  php echo $_FILES [ 'image' ][ 'type' ] ?> 

Follow tutorialspoint

Previous article: Send Email using PHP

Next lesson: Standard writing code in PHP

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile