Simple PHP code for file uploading process

By Faruque Ahamed Mollick

Using PHP it is very easy to making a file uploading system on the server. Today in this post, I am going to show you how to upload file on the server and keep that file in a specific directory.

Your server should have the uploading permission On in php.ini file. The below line of code is responsible for file upload permission which should be inside php.ini file:

file_uploads = On

If the above line of code does not exist in php.ini then you have to write it manually. But in most of the cases, the server already has this by default. So most likely, you don’t have to worry about this. You can ignore the above part and continue with the below part of this post.

PHP file uploading code

At first, you need an HTML uploader form. Below is the HTML file uploader form:

<form enctype="multipart/form-data" action="index.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="51200000" />
    Upload this file: <input name="getfile" type="file" />
    <input name="submit" type="submit" value="Upload File" />
</form>

After that, you have to process the form using PHP code. Before doing it you have to create a directory where you want your files to be moved. Here in this example, the directory is ‘images’. Below is the PHP code that processes the form and moves the file in ‘images’ directory.

<?php
if(isset($_POST['submit'])) {
// Chose directory where your file will be uploaded.
$dir = 'images/';
$randomnum = rand(10000000, 99999999);
// Get the filename
$filename = $randomnum . basename($_FILES['getfile']['name']);
echo $filename;
$fileupload = $dir . $randomnum . basename($_FILES['getfile']['name']);

if (move_uploaded_file($_FILES['getfile']['tmp_name'], $fileupload)) {
  echo "<br/>Your file was successfully uploaded.\n";
  
} else {
   echo "File upload failed";
}
}
?>

Save the above PHP code in a PHP file called index.php. If you change the file name then you also have to change form action parameter.

You can notice in the above code that I have added a random number to the file name that is going to be saved inside images directory. The purpose of doing this is because if the name of two files is same then to make it unique from each other, a unique number will be added with the file name.



Complete code in one PHP file

Below is the complete code within one PHP file:

<?php
if(isset($_POST['submit'])) {
// Chose directory where your file will be uploaded.
$dir = 'images/';
$randomnum = rand(10000000, 99999999);
// Get the filename
$filename = $randomnum . basename($_FILES['getfile']['name']);
echo $filename;
$fileupload = $dir . $randomnum . basename($_FILES['getfile']['name']);

if (move_uploaded_file($_FILES['getfile']['tmp_name'], $fileupload)) {
  echo "<br/>Your file was successfully uploaded.\n";
  
} else {
   echo "File upload failed";
}
}
?> 



<form enctype="multipart/form-data" action="index.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="51200000" />
    Upload this file: <input name="getfile" type="file" />
    <input name="submit" type="submit" value="Upload File" />
</form>

Now run the above code on your server. Remember, before you run this code you need to create a directory called ‘images’ where all the uploaded file will be moved and store.

After you choose and upload a file from your computer if you check the images directory, you will see that file is there inside your server’s images directory or inside the directory that you choose.

So how was that? Is that clear to you? I suggest you try this code on your server. If you have any question related to this post then you can ask me through the comment box.

Leave a Reply

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