Last updated on December 28th, 2022 at 09:51 am

The script below can be use to upload a file to the server. You can make of this script to provide your visitors an option to upload to you web application. This script allows any files to be uploaded(Caution: Make sure that you are strictly checking contents uploaded by your visitors since some extenstions can be malicious. )

Modify your max upload file size limit in PHP.INI according to your requirement.

Complete PHP Code with Form

<?php
session_start();
if(!isset($_POST['upload'])) {
echo '<form name="upload" enctype="multipart/form-data" method="POST" action="'.$_SERVER['REQUEST_URI'].'">
<input type="file" name="file" size="13" value=""/>
<br /><input type="submit" name="upload" value="Upload"/>';
} else {
$yourdomain = 'http://localhost/';
$uploaddir = 'uploads/';
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmpname_file = $_FILES['file']['tmp_name'];

if($filesize[0] > '1000000') {
echo "Way too big!!";
}
else {
	echo "File Size Looks Good<br>";
}
if(move_uploaded_file($tmpname_file, "$uploaddir$filename"))
{
echo "<font color='green'>Successful.<br /><b>URL: </b>".$yourdomain.$uploaddir.$filename."</font>";
}
else
{
	echo "<font color='red'>Some error happened, Check the logs</font>";
}
  }
?>

Modify the below variables, filesize is verified in bytes. So watch out for the size of file you allow visitors to upload. Make sure your domain is correct.

$yourdomain = 'http://localhost/';
$uploaddir = 'uploads/';
$filesize[0] > '1000000'

Once the file is uploaded you can easily get the URL to download from this part of the code.

echo "Successful.<br /><b>URL: </b>".$yourdomain.$uploaddir.$filename."";

Validate the permissions of your uploads/ directory to make sure that there is no error related to permission. Check the web server or PHP logs for any other issues.

This section checks for any errors while uploading like permission denied or may be a folder for upload is not present/found

if(move_uploaded_file($tmpname_file, "$uploaddir$filename"))
{
echo "<font color='green'>Successful.<br /><b>URL: </b>".$yourdomain.$uploaddir.$filename."</font>";
}
else
{
	echo "<font color='red'>Some error happened, Check the logs</font>";
}

Just to reiterate, as we discussed above we are also doing a filesize check to make sure that uploaded files doesn’t exceed the permitted size limit. This is very important and prevent users from simply uploading large files and eating up disk space.

if($filesize[0] > '1000000') {
echo "Way too big!!";
exit;
}
else {
	echo "File Size Looks Good<br>";
}

You may also be interested in taking a look at Drag and Drop upload script using PHP /HTMl5 (HTML5 Drag And Drop Multiple Files And Upload Using PHP)

If the Uploads directory is not available then you may get error similar to

2022/12/28 17:35:09 [error] 3106170#3106170: *84356 open() "/var/www/uploads/test_image_upload.jpg" failed (2: No such file or directory), client: 9xx.xx9.245, server: mistonline.com, request: "GET /uploads/test_image_upload.jpg HTTP/1.1", host: "mistonline.in"

Page looks like this once a file gets successfully uploaded

2 thoughts on “How to upload file using PHP”

Leave a Reply

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