In this tutorial, we will learn basic of uploading of a file in php and also basic configuration required for php file upload.
Configure the php.ini for large file upload
By default php support 2 mb of file upload but in real world we require more than 2 mb. Same as file size php execution time is limited to 30 seconds.
So, Firstly we will change these two settings by which we will be able to upload files greater than 2 mb.
Read this article for how to change and find the php.ini and variables:
; Whether to allow HTTP file uploads.
file_uploads = On
; Maximum allowed size for uploaded files.
upload_max_filesize = 20M
; Maximum size of POST data that PHP will accept.
post_max_size = 20M
max_execution_time = 300
Here, we are changing the execution time of php to 300 seconds, file size to 20 MB and post request size to 20 mb as well.
Now, let’s create the real example of file uploading by following below simple steps:
Step 1 . Create a html file
First step is create html form which support file uploading. To upload a file in a form it’s much important to add the enctype=”multipart/form-data” otherwise form uploading will not work.
Here is the file index.php
<!DOCTYPE html>
<html>
<head>
<title>Readerstacks- PHP File Upload</title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div class=“row”>
<input type="file" id="upload" name=“image”>
</div>
<input type="submit" name=“submit_btn” value="Upload" />
</form>
</body>
</html>
In the above html form tag and form tag contains method=“POST” , action=”upload.php” and enctype=”multipart/form-data”.
We can only upload in method=“POST” and enctype=”multipart/form-data” otherwise, it will not work.
action=“upload.php” is the file name of php script.
Step 2 : Create a php file to handle the form upload
Now, we are going to create a file upload.php and handle the logic of form submit.
<?php
$message="";
if (isset($_POST['submit_btn']) && $_POST['submit_btn'] == "Upload") {
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size']; // if you want to check the file size(ex: 20 mb)
$file_type = $_FILES['image']['type'];// if you want to check the file type(jpg, png,zip)
$uploadFileDir = './uploaded_files/';
$newFileName= rand(9999,99999999).time()."_".$file_name ;
$dest_path = $uploadFileDir . $newFileName;
if(move_uploaded_file($fileTmpPath, $dest_path))
{
$message="File is successfully uploaded.";
}
else
{
$message = "There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.";
}
}
else{
$message = "There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.";
}
echo $message ;
}
else{
echo "Not a form";
}
Here, In first line we are checking $_POST[‘submit_btn’] is set and value is Upload, then we fetch the values from $_FILES variable.
Then, we are creating a dynamic unique name for file so it won’t conflict with other.
$newFileName= rand(9999,99999999).time()."_".$file_name ;
After it , we are using move_uploaded_file function to move the file from temporary location to uploaded_files folder location.
Make sure uploaded_files folder is in same directory where php file is exist and permission are sufficient to upload the file. Permission should be:
Php user and group of folder
$ sudo chown -R www-data:www-data ./uploaded_files
Php folder permission
$ sudo chmod -R 755 ./uploaded_files
Screenshot: