File Upload in PHP



It is very important to know how to upload a file in PHP.File upload in PHP can used for uploading any types of files from a user computer to server. First it will store the file to a temporary folder in server and this file will be deleted when the user exist the script ( leaving that page ). And we can move that file to a permanent folder in server using move_uploaded_file() function.

First see how a HTML form will look like for file upload

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload" id="file" />
<input type="submit" name="submit" value="Upload file" />
</form>

This form will call the file upload.php where we will write the PHP scripts for handling this file upload.

$_FILES array :
$_FILES variable array will contain all the information about the uploaded file like its name, type, size and error codes.
See the different syntax and usage :

  • $_FILES[“form field name for file”][“name”] : It will contain the name of the uploaded file
  • $_FILES[“form field name for file”][“type”] : It will contain the mime type of the uploaded file
  • $_FILES[“form field name for file”][“size”] : It will contain the size of the uploaded file
  • $_FILES[“form field name for file”][“tmp_name”] : It will contain the name with path of the temporary folder in the server.

Now we can write a sample PHP program using the same html form above

<?php
if ($_FILES["fileupload"]["error"] > 0)
{
echo "Error code when upload: " . $_FILES["fileupload"]["error"] . "<br />";
}
else
{
echo "Upload file name : " . $_FILES["fileupload"]["name"] . "<br />";
echo "Type of the file: " . $_FILES["fileupload"]["type"] . "<br />";
echo "Size of the file in KB: " . ($_FILES["fileupload"]["size"] / 1024) . "<br />";
echo "Stored in temporary folder : " . $_FILES["fileupload"]["tmp_name"];
}
?>

Its only a temporary storage now, the file will be deleted when we quit the script. If we need to store the file in server for later use, we need to move that file from temporary folder to a permanent folder ( lets us take a folder “uploads” for this example ). Syntax of the move_uploaded_file(0 function is like
move_uploaded_file( source file, destination file )

Now watch the sample PHP program for file upload to a server and store the file to a permanent folder uploads

<?php
if ($_FILES["fileupload"]["error"] > 0)
{
echo "Error code when upload: " . $_FILES["fileupload"]["error"] . "<br />";
}
else
{
echo "Upload file name : " . $_FILES["fileupload"]["name"] . "<br />";
echo "Type of the file: " . $_FILES["fileupload"]["type"] . "<br />";
echo "Size of the file in KB: " . ($_FILES["fileupload"]["size"] / 1024) . "<br />";
echo "Stored in temporary folder : " . $_FILES["fileupload"]["tmp_name"];
move_uploaded_file($_FILES["fileupload"]["tmp_name"],"uploads/".$_FILES["file"]["name"]);
}
?>

Now our uploaded file is stored in the folder named “uploads” in server.

And this is only a basic introduction about file upload, normally we need to do several other checking and operations before the file upload as per the program requirement. For example if its an image upload section in site, we need to check the uploaded file type is image before storing. And we can restrict the maximum size using the $_FILES[“form field name for file”][“size”] variable. And we can also rename the file before storing. I will explain those things in later posts.