How to populate input type file value from database in PHP?

How to populate input type file value from database in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Why <input type="file"> Cannot Pre-fill from a Database

You cannot populate the value attribute of an <input type="file"> element using a database string. Your current code attempts to echo a filename into the value attribute, but browsers explicitly ignore that data when rendering the file input. This behavior is intentional and stems from how file inputs interact with the operating system's file picker dialog.

When a user selects a file, the browser captures the file path on the client side. Upon form submission, this path is often obfuscated or replaced by a unique identifier (like C:\fakepath\) in the request payload to prevent cross-site scripting attacks and hide local paths from the server. Consequently, there is no persistent value stored in the database that represents the actual file path selected by the user.

To solve the problem of preserving a file selection during an edit operation, you must understand the distinction between the filename and the file path. If your goal is to allow a user to see what file was previously uploaded so they can choose not to change it, you should populate the name attribute or simply rely on the fact that the browser will remember the last selected file if the page is cached or revisited without a full reload. However, if you need to enforce a specific selection based on previous data, you must handle this logic entirely on the server side.

The Limitation of HTML5 File Inputs

The HTML5 specification for <input type="file"> does not support setting a default value string that represents a file path. Even if you manage to set the attribute in your PHP code:

<input class="picturebox" id="logo" name="userfile"  value="<?php echo $discount_details->picture_name; ?>" />

The rendered HTML might look like this:

<input class="picturebox" id="logo" name="userfile"  value="logo_v1.png" />

However, most modern browsers will strip the value attribute or render it as empty when displaying the file input to the user. This is a security feature designed to ensure that users are always explicitly choosing a file rather than being silently forced into a directory or path stored on your server. Attempting to bypass this by manipulating the DOM after the page loads (e.g., using JavaScript) is generally considered an anti-pattern in web development because it breaks the principle of explicit user consent for file uploads.

The Server-Side Solution: Hidden Input Pattern

The standard and secure way to associate a specific file with a form submission while maintaining a clean UI is to use a hidden input field paired with your visible file input. This approach allows you to send the filename or path back to the server even though the browser's file picker doesn't report it in the value attribute.

You can implement this by creating a hidden input that holds the current filename from your database and keeping the visible input empty:

<!-- Hidden field to carry the filename -->
<input type="hidden" name="current_filename" value="<?php echo $discount_details->picture_name; ?>">

<!-- Visible file input (value is intentionally left empty) -->
<input class="picturebox" id="logo" name="userfile" type="file" />

When the form is submitted, your server script can check both fields. If current_filename matches a file in storage and userfile is empty, you know the user did not change their upload. If userfile contains a new file, it overrides the previous one.

Handling File Paths vs. Filenames

Be careful when populating these hidden fields. Storing absolute server paths (e.g., /var/www/html/uploads/logo.png) in the database is bad practice and should be avoided. Instead, store the filename only (logo.png) or a unique identifier associated with that file.

If you need to display the current selected file to the user before submission to give them context, you can use JavaScript to read the files property of the input element when it changes:

document.getElementById('logo').addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
        // Display filename or URL for preview purposes only
        console.log(file.name); 
    }
});

This does not alter the HTML value attribute but provides immediate feedback to the user. Remember that accessing local files via JavaScript requires modern browsers to grant permission, and you cannot reliably use this to populate the input's value attribute directly for persistence across reloads.

Conclusion

The inability to pre-fill <input type="file"> is a browser security feature, not a limitation of PHP. To achieve the desired behavior in your editing forms, rely on hidden inputs to track the filename and handle the logic of file replacement on the server side. This ensures that users retain their previous selections by default while still having the ability to update them explicitly if needed.

Our network