You can create file upload functionality by using PHP. Initially, files are uploaded into a temporary directory of the web server and then relocated to a target destination folder by a PHP function.

File upload parameters, i.e. upload_tmp_dir, upload_max_filesize are default set into PHP configuration file php.ini.

Create an HTML Upload-File Form

<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="resume" id="resume">
<input type="submit" name="SubmitBtn" id="SubmitBtn" value="Upload Resume">

HTML form attribute (enctype='multipart/form-data') is required to upload a file.

Creating an PHP File Upload Script




      //New file name

      //File upload path

      //function for upload file
        echo "Successful"; 
        echo "File Name :".$newFileName; 
        echo "File Size :".$fileSize." kb"; 
        echo "File Type :".$fileType; 
      echo "Maximum upload file size limit is 200 kb";
    echo "You can only upload a Word doc file.";

By using the PHP global $_FILES array you can upload file from a client computer to the web server.

  • $_FILES["file"]["name"] - uploaded file name
  • $_FILES["file"]["type"] - uploaded file type
  • $_FILES["file"]["size"] - uploaded file size in bytes
  • $_FILES["file"]["tmp_name"] - uploaded file temporary file name
  • $_FILES["file"]["error"] - the error code resulting from the file   upload

Saving the Uploaded File

The examples above create a temporary copy of the uploaded files in the PHP temp folder on the web server and then PHP move_uploaded_file() function relocate uploaded file from temp directory to a our target destination.

The temporarily copied file auto-deleted when the script execution ends.

Restrictions on Upload

The examples above used some validations to ensure file upload as per the requirement.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram