How to display uploaded file name in magento admin form?

How to display uploaded file name in magento admin form?

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to display uploaded file name in magento admin form?

How to Display Uploaded File Name in Magento Admin Form

As a senior developer at LaravelCompany, I often encounter this common challenge when working on custom Magento modules—displaying the name of an uploaded file in the admin form after a successful upload.

Understanding the Challenge

When you create a custom form in your Magento module, you might want to allow administrators to upload a file (like a logo or document). After they successfully upload it, you need a way to display either the file name or a link to that file on this same form. This is especially important for keeping track of which file is currently attached.

The Solution: Using Hidden Inputs and Display Fields

The key to solving this puzzle lies in using a hidden input field to store the file’s path after upload, and then displaying the file name in an additional input field or label on your form. Here’s how you can implement this:

Step 1: Add a Hidden Input Field for the File Path

In your form XML layout, add a hidden input field with a unique ID to store the uploaded file path. This will look something like this:

<field id="file_path" type="text" translate="label"
       sort_order="10" show_in_default="1" show_in_website="1">
    <label>File Path</label>
</field>

Step 2: Handle the File Upload

In your controller’s save action, handle the file upload using Magento’s built-in move_uploaded_file function. After a successful move operation, get the new file path and assign it to the hidden input field:

if (isset($_FILES['file'])) {
    $uploader = new \Magento\Framework\Image\Adapter\Imagick();
    try {
        $result = $uploader->save($_FILES['file']);
        if ($result) {
            $data['file_path'] = $result;
        }
    } catch (\Exception $e) {
        // Handle exception
    }
}

Step 3: Display the File Name

Next, in your form.phtml view file, you can use PHP to retrieve the value from the hidden input field and display it in a label or another input field:

<?php if ($block->getRequest()->getParam('file_path')) : ?>
    <div class="admin__field field type-text required">
        <label for="file_name" class="admin__label field-label"><?= __('File Name') ?></label>
        <input name="file_name" id="file_name" type="text"
               value="<?= $block->getRequest()->getParam('file_path'); ?>"
               readonly="readonly"/>
    </div>
<?php endif; ?>

In this example, we’re checking if the file_path parameter is set in the request. If it is, we display a read-only text field containing the file name.

Best Practices

  1. Use Read-Only Fields: Make the displayed field read-only to prevent accidental edits.
  2. Error Handling: Always include proper error handling for file upload operations.
  3. File Security: Ensure your file uploads are secure by validating and sanitizing input, checking file types, and using appropriate permissions.

Conclusion

Displaying an uploaded file name in a Magento admin form is a common task that can be easily accomplished with hidden inputs, PHP logic, and some careful layout design. By following the steps outlined above, you can create a seamless user experience for your administrators by providing clear visibility of which file is currently attached to their records.

Remember, at LaravelCompany, our team of expert developers specializes in building custom Magento solutions. If you need assistance with these or any other Magento-related challenges, don’t hesitate to reach out!

Happy coding!

[Learn more about optimizing your Magento performance on our blog: https://laravelcompany.com/blog/magento-performance-optimization/]

Extensions Themes Admin Orders

Our network