How to get the file path in html <input type="file"> in PHP?

How to get the file path in html <input type="file"> in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why $_GET Cannot Access Uploaded Files and How to Fix It

The core issue in your code stems from a fundamental misunderstanding of how web browsers handle file inputs versus form data. You are attempting to pass the physical file path of a CSV using the name="csv_file" attribute, but this mechanism simply does not work for security reasons.

When you use <input type="file">, the browser never sends the local filesystem path (e.g., C:/Users/You/Documents/data.csv) to your server. Instead, it uploads the file contents to a temporary location on the web server and provides a unique identifier known as a "tempnam." This identifier is what you must use to read the file, not the original client-side path.

Your current implementation attempts to access $_GET['csv_file']. In your HTML form, the action method is set to get, which appends data to the URL query string. However, browsers strictly block file inputs from submitting their values via GET requests for security and privacy reasons. Even if you switched the form method to POST, $_GET would still remain empty because the input name is associated with the $_FILES superglobal array, not $_POST or $_GET.

To resolve this, you need to switch your form submission method to method="post" and access the uploaded file via the $_FILES superglobal. This array contains metadata about the upload, including the temporary filename where the server placed the file after receiving it.

Here is how you should structure the form in index.php:

<form action="csv_to_database.php" method="post" enctype="multipart/form-data">
    <input type="file" name="csv_file" />
    <input type="submit" name="upload" value="Upload" />
</form>

The addition of enctype="multipart/form-data" is mandatory. Without this, the form data cannot be parsed correctly when a file is involved, regardless of whether you are using POST or GET.

In your processing script csv_to_database.php, you must check if the upload was successful and then use the temporary filename provided in the array. Do not attempt to read from $_FILES['csv_file']['name'] immediately; that value contains the original filename sent by the client, which might be malicious or non-existent on your server. Instead, use $_FILES['csv_file']['tmp_name'].

<?php

if (isset($_POST['csv_file']) && isset($_FILES['csv_file'])) {

    $row = 1;

    // Check if the upload was successful before attempting to open the file
    if ($_FILES['csv_file']['error'] === UPLOAD_ERR_OK) {
        
        $handle = fopen($_FILES['csv_file']['tmp_name'], "r");

        if ($handle !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                echo "<p> $num fields in line $row: <br /></p>\n";
                $row++;
                for ($c=0; $c < $num; $c++) {
                    // Escape output to prevent XSS vulnerabilities
                    echo htmlspecialchars($data[$c]) . "<br />\n";
                }
            }
            fclose($handle);
        } else {
            echo "Failed to open the uploaded file.";
        }

    } else {
        // Handle specific upload errors (e.g., file too large, no temp directory)
        $error = array(UPLOAD_ERR_OK => 'No error',
                       UPLOAD_ERR_INI_SIZE => 'Exceeds system ini file size limit',
                       UPLOAD_ERR_FORM_SIZE => 'Exceeds form MAX_FILE_SIZE directive',
                       UPLOAD_ERR_PARTIAL => 'File was only partially uploaded',
                       UPLOAD_ERR_NO_FILE => 'No file was uploaded',
                       UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
                       UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
                       UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload");

        echo "Error: " . $error[$_FILES['csv_file']['error']];
    }

} 
?>

Security Considerations and Best Practices

While this approach allows you to read the CSV data, relying on client-side uploads for direct database insertion or processing carries inherent risks. The path you are using (tmp_name) is temporary; once the script finishes executing or the session ends, the file is deleted. If your application needs to persist the file, you must move it from the temporary directory to a permanent location using move_uploaded_file().

Furthermore, always validate the MIME type of the uploaded file before processing it. A user could rename a malicious executable to .csv and upload it. You should verify that the content matches your expectations using finfo_file() or checking the $_FILES['csv_file']['type'] property against an allowlist.

If you are building complex applications involving file handling, consider using a framework like Laravel. It provides robust facades for managing uploads and validating files automatically, reducing the likelihood of security oversights. For more details on secure file handling patterns in modern PHP ecosystems, resources such as https://laravelcompany.com offer comprehensive guides on leveraging built-in helpers to manage $_FILES data safely.

Our network