PHP - File uploading

    You can upload a file to the server by using a PHP script with HTML forms. Before uploading the file to the original destination it gets uploaded to a temporary directory by a PHP script. The temporary directory information is mentioned in the phpinfo.php page which is specified as upload_tmp_dir. You can mention the maximum size for the uploaded file using upload_max_filesize which can be set in the php.ini file. You can upload a file by using the below steps-

    • First, the user opens an HTML form page that may contain a browse option and the submit option.
    • Once you click the browse option, you can select any file that you want to upload from your system.
    • After you select a file, it will automatically take the file complete path.
    • Then this selected file will store in the temporary location.
    • Then PHP script checks if file reaches the temporary location and then moves it to the destination.
    • Once the uploading of the file is successful, it will return success to the user.

    In case if you want to perform any writing operations on the file then both temporary and the destination folder should have the permissions.

    How to create an update form using PHP

    In the below HTML code example we are creating a form that has a method as POST and multipart/form-data as the value of the enctype attribute.

    Example:

    <?php
    if(isset($_FILES['image'])){
    $errors=array();
    $file_name =$_FILES['image']['name'];
    $file_size =$_FILES['image']['size'];
    $file_tmp =$_FILES['image']['tmp_name'];
    $file_type=$_FILES['image']['type'];
    $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
    $extensions=array("jpeg","jpg","png");
    if(in_array($file_ext,$extensions)=== false){
    $errors[]="extension not allowed.";
    }
    if($file_size > 2097152){
    $errors[]='File size must be exact 2 MB';
    }
    if(empty($errors)==true){
    move_uploaded_file($file_tmp,"images/".$file_name);
    echo "Success";
    }else{
    print_r($errors);
    }
    }
    ?>
    <html>
    <body>
    <form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image" />
    <input type="submit"/>
    </form>
    </body>
    </html>

    Output:

    File Uploading

    How you can create an upload script with PHP

    You can keep all the information related to an uploaded file using a two-dimensional array which is global PHP variable, $_FILES . There are five different variable combinations that can be used with this global variable-

    • $_FILES['file']['tmp_name'] ? this will specify the temporary directory of an uploaded file on the web server.
    • $_FILES['file']['name'] ? this variable specifies the actual name of the uploaded file.
    • $_FILES['file']['size'] ? this variable specifies the size in bytes of the uploaded file.
    • $_FILES['file']['type'] ? this variable specifies the MIME type of the uploaded file.
    • $_FILES['file']['error'] ? this variable specifies the error code associated with this file upload.

    People are also reading: