Uploading files on a web server can be tricky as it requires a little configuration in the php.ini file (found on the server configuration path). So it is important to be cautious when uploading a file.

Enable file upload on server

The directive in the php.ini files should be set to “on”, like this:

file_uploads = On;

If the line exists but it's commented out, remove the comment slashes, like this:

// file_uploads = On;

make it:

file_uploads = On;

The most common way of uploading files is by creating a form structure in HTML. A form allows user to choose an image file or a regular file type.

Example of a HTML form file uploaded and processed with PHP

<!DOCTYPE html>
<html>
<body>
    <form action="execute_file_upload.php" method="post" enctype="multiform/fomr-data">
        Select file:
        <input type="file" name="file_uploaded" id="file_uploaded" />
        <input type="submit" value="submit" name="submit" />
    </form>
</body>

The post method in the previous example is used to post the message to the server. It is an essential part of the script to use with the form. The attribute “enctype” needs to be used as an attribute which specifies the content-type to use when submitting a form. These attributes are required to upload a file. The type “file” attribute shows the input field as a selection control with the browser button next to the input control. The form generated above will be saved in the "execute_file_upload.php". We need to create the "execute_file_upload.php" file in order to upload the file to the server. The example is given as follows for "execute_file_upload.php".

Example with uploading files server script

<?php
    if (isset($_POST["submit"])) // check if a file has been submitted and if it is an image
    {
        $dir = "uploads/"; // directory where files are going
        $file = $dir . basename($_FILES["file_uploaded"]["name"]); // file's path and name uploaded
   
        $file_type  = pathinfo($file, PATHINFO_EXTENSION); // check file's type
        if ($file_type == "PNG" || $file_type == "JPG" || $file_type == "GIF" || $file_type == "JPEG")
       {
           echo "File is an image  " . $file_type . ".";
       }
       else 
       {
            echo "File is not an image!";
       }
   }
?>

We can also use a code to check if the file already exist prior uploading:

Example with checking if a file exist

<?php
    if (isset($_POST["submit"])) // check if a file has been submitted and if it is an image
    {
        $dir = "uploads/"; // directory where files are going
        $file = $dir . basename($_FILES["file_uploaded"]["name"]); // file's path and name uploaded
   
       if (file_exists($file)) // check if file exists
       {
           echo "File already exists";
       }
       else {
           // ... check type, size..., UPLOAD
       }
   }
?>

The file size and type can also be defined by using PHP code.

Example of checking a file's size

<?php
    if (isset($_POST["submit"])) // check if a file has been submitted and if it is an image
    {
        $dir = "uploads/"; // directory where files are going
        $file = $dir . basename($_FILES["file_uploaded"]["name"]); // file's path and name uploaded
        
        $size = $_FILES["file_uploaded"]["size"];
   
       if ($size > 800000) // check if the size above 800 kb
       {
           echo "File is too big";
       }
       else {
           // ... check type, ..., UPLOAD
       }
   }
?>

Superglobals

Superglobals are predefined variables always accessible in PHP regardless of the scope. They can be accessed from any class or function.

The most important super global are:

  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES

 

›› go to examples ››