PHP File Upload



PHP File Upload

In PHP file upload concept used to upload files to the server.

we need to upload file in web application such as upload resume in job portal site, upload profile picture in social networking site, upload images in gallery and upload file as attachment for send mail.
All this kind of uploading we use file upload concept to upload image file, text file or any document to the server.

HTML design form for upload file.

PHP File Upload Note :

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

HTML upload.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 above script :

PHP File upload
PHP File upload example

When click upload button the “fileupload.php” script will execute, write upload file code in fileupload.php script file.

The fileupload.php script code

<?php

if(isset($_POST['Submit1']))
{ 
$filepath = "images/" . basename($_FILES["file"]["name"]);

if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) 
{
echo "The file ". basename( $_FILES["file"]["name"]). " uploaded..";
} 
else 
{
echo "Error !!";
}
} 

?>

IF the image uploaded successfully then display success message otherwise display error message.


Check File already exists or not

when we upload a file, if file already exists in directory then it will give us some error message.
Here, for check existence of file we use php file_exists() function.

<?php

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

if(file_exists($filepath))
{
echo "Error!!, file already exists";
}
else
{
// write uploading code here
}

?>

Check file type

Some time we allow to upload only jpeg and gif image file, so we need to check file type.
In php we use $_FILES[“file”][“type”] to check the file type.

<?php

if($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/gif")
{
//write upload code
}
else
{
echo "Error !!, Only jpeg and gif image allowed";
}

?>

Check file size

We know that many website restricted to upload larger file. For make restriction on upload larger file we need to check file size before uploading file.
In php $FILE[“file”][“size”] used to check the file size in bytes.

Example of check file size

We assume that the uploaded file must be smaller than 800kb.

<?php

if($FILE["file"]["size"]<=800000)
{
//write upload code 
}
else
{
echo "Error!! file must be smaller than 800kb";
}

?>

Now, lets take an example to check file type, file size and file directory where file uploaded.

PHP file upload example

<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>

<?php

if(isset($_POST['Submit1']))
{ 
$filepath = "images/" . $_FILES["file"]["name"];

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 !!";

}
}

?>
</body>
</html>

The output of file upload example is :

PHP File uplaod
PHP File Upload Example

Leave a Reply

Your email address will not be published. Required fields are marked *