How to Upload File in PHP? A Definitive Guide For Every Beginner

File upload is an important aspect in every web based application. Doing it with minimal code could actually save your time. So, PHP has an easiest way to add images, files, & documents to the server. In this article, we will provide a PHP script to upload File in PHP. You can use it to add several files including images to the PHP server.

Upload Form HTML – Upload File in PHP

Firstly, you need to develop an HTML file. This file facilitates access to pick out a file you need to use. Make sure that your tag includes the following attributes: 

  • method=”post”
  • enctype=”multipart/form-data”

Besides, you must make sure the <input> tag includes the type=”file” attribute. 

Your code will look alike this:

<pre class="wp-block-code"><code lang="markup" class="language-markup line-numbers"><form action="uploadfile.php" method="post" enctype="multipart/form-data">
    Select File to Upload:
    <input type="file" name="file">
    <input type="submit" name="submit" value="Upload">
</form></code></pre>

So, the upload form above is submitted to the uploadfile.php file. You can use it to submit an image or add document server. 

The next step is to create a document. Further, you can use it to upload file in PHP.

You can move uploaded files using move_uploaded_file() function. So, the following code is used to add documents in PHP. Make sure you define the directory location in $target_dir. It is a variable where you can place the uploaded file.

Also, we have mentioned various file types in the following code. You can upload them in the directory. 

<?php
$status_msg = '';
$target_dir = "uploads_files/";  // File upload aboutsulte path For Example : C:/xampp/htdocs/php-demos/uploads_files/
$filename = basename($_FILES["file"]["name"]); // get your file base name
$target_file_path = $target_dir . $filename; // set your path with file to upload
$filetype = pathinfo($target_file_path,PATHINFO_EXTENSION); // get your file type 
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])) {
    //allow certain file formats
    $allowed_file_types = array('jpg','png','jpeg','gif','pdf','doc','docx','ppt','pptx'); // Set your allow file types here
    if(in_array($filetype, $allowed_file_types)){
        //upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_path)){
            $status_msg = "The file ".$filename. " has been uploaded successfully.";
        }else{
            $status_msg = "Sorry, there was an error while uploading your file.";
        }
    }else{
        $status_msg = 'Sorry, only JPG, JPEG, PNG, GIF, PDF, DOC, DOCX, PPT, PPTX files are allowed to upload.';
    }
}else{
    $status_msg = 'Please select a file to upload.';
}

//display file upload status message
echo $status_msg;
?>

So, This is how you upload file in PHP. The above coding will bring ease to your tasks. From now onwards, you can easily add images and every other document to the PHP server. You can add jpg, jpeg, png, pdf, doc, PPT, PPTX files effortlessly. All these files are allowed in the server-side. So, create great web based applications using this simple coding. Let me know whether everything worked out great or not. 

Also Read, WooCommerce 10 PHP Snippets to Increase Sales

Everything that You Should Know About What is PHP

Important links: PHP – https://www.php.net/