Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to access actual name of uploaded file in PHP ?

How to access actual name of uploaded file in PHP ?

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:

RELATED ARTICLES

Most Popular

Recent Comments