Is it Possible to make a button as File upload Button?

Is it Possible to make a button as File upload Button?

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

Stefan Bogdanescu

Founder & Senior Architect

Can a Button Act as a File Upload Control?

The short answer is yes, but it requires changing how you think about HTML forms and server-side processing. In standard HTML, a button element (<button> or <input type="submit">) triggers an action, while a file input (<input type="file">) captures data. They serve fundamentally different roles in the form submission lifecycle.

To achieve the behavior where clicking "Upload MB" immediately opens the file selection dialog without navigating to a new page, you must replace the button element entirely with a hidden file input and trigger it programmatically. You cannot make a standard submit button behave like a file picker because the browser does not natively associate click events on a button with file selection dialogs.

The Technical Implementation

The solution involves three specific steps: hiding the default file input, placing it immediately adjacent to your visible "Upload MB" button, and using JavaScript to intercept the click event.

First, ensure your HTML structure looks like this:

<form action="/upload-handler" method="POST" enctype="multipart/form-data">
    <!-- Hidden file input -->
    <input type="file" name="file_to_upload" id="hidden-file-input" style="display: none;">
    
    <!-- Visible trigger button -->
    <button type="button" onclick="triggerUpload()">Upload MB</button>
</form>

<script>
function triggerUpload() {
    document.getElementById('hidden-file-input').click();
}
</script>

In this setup, the <input type="file"> element is set to display: none;. This hides it visually but keeps it active in the DOM so the browser can still handle file selection logic. When the user clicks "Upload MB", the JavaScript function executes and programmatically triggers a click on the hidden input field. The browser then responds exactly as if the user had clicked the native file picker.

Handling Form Submission

This approach works seamlessly for the initial file selection phase. However, you must manage the final submission carefully. When the user selects a file via your custom button, the form does not submit automatically yet; the file is merely selected and stored in the input's data attribute.

To complete the upload process, you need a mechanism to actually send the data to the server. Common patterns include:

  1. Manual Submit: Adding a separate "Confirm Upload" button that submits the form. This keeps the UI clean until the user is ready.
  2. Auto-Submit: Using JavaScript to submit the form immediately after file selection.
    function triggerUpload() {
        const fileInput = document.getElementById('hidden-file-input');
        if (fileInput.files.length > 0) {
            // Optional: Validate or process the file here
            fileInput.form.submit();
        } else {
            alert("Please select a file first.");
        }
    }
    

Server-Side Considerations

From a PHP perspective, $_FILES will behave identically regardless of whether the file was selected via a native input or a JavaScript-triggered hidden input. The key is ensuring your server-side handler (e.g., /upload-handler) correctly processes the multipart/form-data payload.

One potential pitfall is browser security restrictions regarding programmatic form submission. Some browsers may block auto-submission if the form does not have valid data or if the user has previously interacted with it in a specific way that flags it as an automated action. However, since the file input is explicitly clicked by the user (via your script), this usually isn't an issue.

When to Use This Pattern

This pattern is particularly useful for Single Page Applications (SPAs) or legacy sites where you want to maintain a specific design without relying on JavaScript-heavy frameworks like React or Vue to handle native file inputs. It preserves accessibility because the underlying element remains a standard <input type="file"> that screen readers can identify, even if it is visually hidden.

If you are building a modern Laravel application, you might consider using Tailwind CSS classes to hide the input and utility scripts to trigger it, ensuring your code remains clean and maintainable. For more complex file handling workflows, such as drag-and-drop or image previews before upload, libraries like dropzone or native HTML5 features often provide better UX than this manual workaround.

Ultimately, while a button cannot natively open a file picker, decoupling the UI trigger from the form element provides the flexibility needed to create custom upload experiences without sacrificing functionality.

Our network