File upload using PHP forms

Spread the love

In File upload using PHP lets directly begin with the example.

Consider below example and follow the steps to perform File upload using PHP.

  1. In the form add file input field.
  2. Set name, action and method attributes

<form name="file_upload" action="uploads.php" method="post" >
<input type="file" name="photo" />
<input type="submit" value="Submit" />
</form>

In above example, we have created a simple form with file input field with type “file” and the name of the field as “photo” to upload a profile photo of employees.

[ad name=”Responsive display and text”]

Step 2:

Add one more attribute in form tag which is “enctype=’multipart/form-data‘”.

<form name="file_upload" action="uploads.php" method="post" enctype="multipart/form-data" >
<input type="file" name="photo" />
<input type="submit" value="Submit" />
</form>

In file upload, after submission of the form, file data is always collected in another superglobal variable which is $_FILES. This array variable is a two-dimensional array. Its first dimension always having the name given to file input field. In our example, it is “photo”. $_FILES is having five type of array values, these are as follows:

  1. $_FILES[‘file_input_name’][‘tmp_name’] – “tmp_name” is the temporary directory name.
    While uploading process works, file first gets uploaded into temporary directory on the server. That directory location is stored in $_FILES[‘file_input_name’][‘tmp_name’] variable.
  2. $_FILES[‘file_name’][‘name’] –  it will contain the name of file which you select or browse from file input.
  3. $_FILES[‘file_name’][‘size’] – it will contain the size of file you have uploaded in bytes.
  4. $_FILES[‘file_name’][‘type’] –  it will contain the MIME type of the file you have uploaded.
  5. $_FILES[‘file_name’][‘error’] – it will contain the error code, if any error occurs while uploading file.

 

Step 3:

And add below PHP code which will handle the file upload functionality. And test the file uploaded in your specified path.

<?php
echo "<pre>",print_r($_FILES),"<pre>";
if(is_uploaded_file($_FILES['photo']['tmp_name']))
{
$file_name = $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'],"/myuploads/".$file_name);
}
?>

<form name="file_upload" action="uploads.php" method="post" enctype="multipart/form-data" >
<input type="file" name="photo" />
<input type="submit" value="Submit" />
</form>

As you have seen in above example we have used $_FILES superglobal array inside a print_r function. It will print all array variables of $_FILES as explained above.

We have also used two more functions which are “is_uploaded_file()” and “move_uploaded_file()”. Let’s see these functions first.

is_uploaded_file():

This function will check whether the file is actually an uploaded file or not. As because of the cookies, the previous files get overwritten.

[ad name=”Responsive Text”]

 

move_uploaded_file():

This function moves the uploaded file into the specified path. This function’s first parameter is the temporary file name of the uploaded file and the second parameter contains the path to the directory where you want to save your file with the file name.

There is one more function named copy(<file name>,<path of  file>). This function copies the file into specified path.


Spread the love

Leave a Comment