start learning
Image 1
577889925852

Superglobal $_FILES in PHP

In PHP, $_FILES is a superglobal variable that is used to handle file uploads submitted via HTML forms. When a user uploads a file through a web form, $_FILES provides information about the uploaded file, such as its name, type, size, and location. This superglobal allows you to process and manipulate uploaded files within your PHP scripts.


Example with Clarification :

Let's create an example to demonstrate how to use the $_FILES superglobal to handle file uploads in PHP and PHP script (upload.php) to handle the file upload using the $_FILES superglobal:

<?php
if (isset($_POST['submit'])) {
    // Get information about the uploaded file
    $fileName = $_FILES['fileToUpload']['name'];
    $fileType = $_FILES['fileToUpload']['type'];
    $fileSize = $_FILES['fileToUpload']['size'];
    $fileTmpName = $_FILES['fileToUpload']['tmp_name'];
    
     // Specify the target directory to move the uploaded file to
    $targetDirectory = "uploads/";
    
     // Create the target path
    $targetPath = $targetDirectory . $fileName;
    
     // Attempt to move the uploaded file to the target directory
    if (move_uploaded_file($fileTmpName, $targetPath)) {
    echo "File '$fileName' has been successfully uploaded.";
    } else {
    echo "Error uploading the file.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP $_FILES Superglobal Example</title>
</head>
<body>
    <h1>File Upload Example </h1>
    <form action="upload.php" method="post" enctype="multipart/form-data">
      <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
    </form>
</body>
</html>
  1. The HTML form above contains an input field of type "file," which allows the user to select a file for upload. The form's enctype attribute is set to "multipart/form-data," which is required for file uploads.
  2. In the PHP script, we first check if the form was submitted by checking if the "submit" button was clicked.
  3. We access information about the uploaded file using the $_FILES superglobal. In this case, we use $_FILES['fileToUpload'] to access information about the file input field named "fileToUpload."
  4. We retrieve details like the file name, file type, file size, and the temporary file location (tmp_name).
  5. We specify the target directory where we want to move the uploaded file (uploads/ in this example) and create the target path.
  6. We use the move_uploaded_file function to move the file from its temporary location to the target directory. If the file upload is successful, we display a success message; otherwise, we display an error message.

This example illustrates how to use the $_FILES superglobal to handle file uploads. It allows you to extract information about the uploaded file and move it to a specified directory. Proper file type validation and security measures should be implemented in a production environment to ensure secure file handling.


×