In this article File Upload in PHP we give the information about File uploading is a common process in PHP, in which users can upload files to the server through a web form.

File Upload and File Permissions in PHP

File uploading is a common process in PHP, in which users can upload files to the server through a web form. Additionally, it is also important to properly manage permissions for files so that only the required users can edit or delete files.

  1. File Upload in PHP

The following procedures are followed to upload files in PHP:

File Upload Example

<?php

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $target_dir = “uploads/”;  // directory to upload

    $target_file = $target_dir .basename($_FILES[“fileToUpload”][“name”]);

    $uploadOk = 1;

    $file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

//check file type

    if ($file_type != “jpg” && $file_type != “png” && $file_type != “jpeg” && $file_type != “gif”) {

        echo “We only accept JPG, JPEG, PNG and GIF file types.”;

        $uploadOk = 0;

    }

// file uploading process

    if ($uploadOk == 1) {

        if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {

            echo “Your file has been uploaded successfully.”;

        } else {

            echo “There was an error uploading file!”;

        }

    }

}

?>

 $_FILES[“fileToUpload”][“name”] : To get the name of the file.

move_uploaded_file() : To upload the file from temporary location to permanent location.

  1. File Permissions

chmod() is used to set the permissions of files and directories. Permissions control various operations (read, write, execute) for the owner, group, and other users of the file or directory.

Setting File Permissions

chmod(“example.txt”, 0777);  // set file permissions

 0777: Provides full permissions (read, write, execute).

0666 :Allows only read and write.

  1. File Permission Example

Checking File Permissions

$perms = fileperms(“example.txt”);

if (($perms & 0x0004)) echo “Nightwings are allowed.”;

if (($perms & 0x0002)) echo “Write is allowed.”;

if (($perms & 0x0001)) echo “Permitted to run.”;

fileperms() : Gets the permissions of a file or directory.

  1. File Upload and Permissions with Validation

When uploading a file, it is important to grant appropriate permissions and update the permissions. Here is a complete example:

File Upload with Permissions

<?php

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

    $target_dir = “uploads/”;

    $target_file = $target_dir .basename($_FILES[“fileToUpload”][“name”]);

    $uploadOk = 1;

    $file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// set permissions (first 0777)

chmod($target_file, 0777);

    //check file type

    if ($file_type != “jpg” && $file_type != “png” && $file_type != “jpeg” && $file_type != “gif”) {

        echo “We only accept JPG, JPEG, PNG and GIF file types.”;

        $uploadOk = 0;

    }

//upload the file

    if ($uploadOk == 1) {

        if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {

            echo “Your file has been uploaded successfully.”;

        } else {

            echo “There was an error uploading file!”;

        }

    }

}

?>

Conclusion

  • Uploading files in PHP is possible using move_uploaded_file().
  • File and directory permissions can be controlled by chmod() so that different users can access the permissions.
  • It is important to set permissions appropriately in the file upload process so that the file is secure and functions correctly.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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