Open In App

How to access actual name of uploaded file in PHP ?

Last Updated : 09 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”].

  • The $_FILES is the by default keyword in PHP to access the details of files that we uploaded.
  • The file refers to the name which is defined in the “index.html” form in the input of the file.
  • The name refers to the actual name of the file.

In this article, we understand how to extract the actual name.

Example: There is a form in the “index.php” file which takes a file as input and then sends it to “file.php” using the POST method and there we can find the name and all other details of the file by using the $_FILES. Always make sure to write the correct name of the file in which you want to send the data. In this, we send the file from “index.php” to “file.php”.

index.php




<!DOCTYPE html>
<html>
  
<body>
    <form action="file.php" method="post" enctype="multipart/form-data">
        Select file to upload:
        <input type="file" name="file"><br>
        <input type="submit" value="Upload file" name="submit">
    </form>
</body>
</html>


file.php




<?php
  
    // Stores the file name
    $name = $_FILES["file"]["name"];
  
    // Store the file extension or type
    $type = $_FILES["file"]["type"];
  
    // Store the file size
    $size = $_FILES["file"]["size"];
  
    echo "File actual name is $name"."<br>";
    echo "File has .$type extension" . "<br>";
    echo "File has $size of size"."<br>";
  
?>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments