PHP File Upload


PHP File Upload


In PHP file upload concept, we will learn how to upload a file from a PHP script. The file can be a simple text file, images, word document or any other document.

Different types of web applications are designed to facilitate the user in uploading their information. For example, a user may have to upload a resume on job portal website or may have to upload a profile picture on social networking sites and matrimonial sites or may have to upload images to a gallery and so on.

To achieve all the above task, file upload concept is used. It uploads the file to the server.

General Steps To Upload a file


1) Design an HTML form that contains a file upload control (file upload control consists of a textbox and browse button) and submit button.

2) The user opens the HTML form and clicks on the browse button and selects the file to be uploaded from their computer (local pc).

3) Once the path of the uploaded file appears in the textbox, a user clicks on the submit button.

4) The uploaded file is stored in the temporary directory of the server.

5) In the PHP script that is specified in the action attribute of the form, checks whether the file has arrived or not. If it has arrived, then it is moved from the temporary directory to the intended directory.

6) Finally, a success message is displayed to the user.

$_FILES

It is the PHP global variable which contains the information about the uploaded file.

It is an associative two-dimensional array where PHP keeps the information about the uploaded file.

It has five variables:

a) name – indicates the actual name of the uploaded file. 

b) size – indicates the size in bytes of the uploaded file. 

c) type – indicates the MIME type of the uploaded file. 

d) error – indicates the error code for the uploaded file. 

e) tmp_name – indicates the full path of the uploaded file (temporary directory path). 

PHP File Upload Note:

  • Make sure the method=”post” 
  • Must use attribute enctype=”multipart/form-data” in the form tag.

HTML fileupload.html script for file upload


<html>

<body>

<form action="FileUpload.php" enctype="multipart/form-data" method="post">

Select image :

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

<input type="submit" value="Upload" name="Submit1">

</form>

</body>

</html>

The output of the above HTML page:




The fileupload.php script code


<?php

if(isset($_POST['Submit1']))



$filepath = "images/" . $_FILES["file"]["name"];

if ($_FILES["file"]["name"] != "")

{

copy ($_FILES["file"]["tmp_name"],$filepath);

/* instead of copy function you can also use
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) 
*/

echo "The file ". basename( $_FILES["file"]["name"]). " uploaded.<br>";

echo "The File Name = " . $_FILES["file"]["name"] . "<br>";

echo "File Type = " . $_FILES["file"]["type"] . "<br>";

echo "File Size = " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

echo "Temporary File Location = " . $_FILES["file"]["tmp_name"];


else 

{

echo "Error in Uploading File";

}

}

?>

The output of the above PHP script:


Conclusion

In conclusion, PHP file upload functionality is a crucial aspect of developing interactive website. Deep understanding of file upload processes in PHP make sures that the website is secure and efficient, at the same time enables users to share and manage different content on websites.

web development, enabling users to share and manage diverse content on websites. Understanding the intricacies of file upload processes in PHP ensures the creation of secure and efficient applications. 
Tags

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. I read a weblog, I hope that it doesn't sadden me as much as this one. I’m talking about, I know it was my selection to read, but I actually thought you'd have something interesting to say. Great work admin..
    Document Management Software Dubai
    Document Management Software
    Document Management Software UAE
    Best Document Management Software

    ReplyDelete