📚 PHP Tutorial

PHP Tutorial

Learn PHP concepts with simple explanations, examples and practical notes.

File Uploading in PHP

PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variable i.e. $_FILES, it contains all information about file.By the help of $_FILES global variable, we can get file name, file type, file size and temparary file name associated with file.


  • $_FILES['filename']['name'] - returns file name.
  • $_FILES['filename']['type'] - returns MIME type of the file.
  • $_FILES['filename']['size'] - returns size of the file (in bytes).
  • $_FILES['filename']['tmp_name'] - returns temporary file name of the file which was stored on the server.

In HTML, File Upload control can be created by using following:

<input type="file" name="fileupload"/>


In PHP, To upload the file we have to use the function called move_uploaded_file().

☞ move_uploaded_file() function

The move_uploaded_file() function is used to move the uploaded file to a new location. It moves the file only if it is uploaded through the POST request.

Syntax:
  move_uploaded_file ( string $filename , string $destination )


First Configure the php.ini File by 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 i.e. file_uploads = On

Example: "fileupload.php"



Output :

image