Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Form File Uploads doesn't upload file

Files doesn't get uploaded though all back-end code is correct. I tested the back end code with another front-end styled code and it worked fine.

but in my front end code it doesn't upload any files. I removed all css and scripts as well to figure out the issue.

here is my simple front end HTML form :

<form action="upload_handler.php" method="post">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>
like image 551
Iresha Rubasinghe Avatar asked Jan 25 '16 04:01

Iresha Rubasinghe


People also ask

How do I allow HTML to upload a file?

HTML allows you to add the file upload functionality to your website by adding a file upload button to your webpage with the help of the <input> tag. The <input type=”file”> defines a file-select field and a “Browse“ button for file uploads.

Why won't Google Forms let me do a file upload?

Turn off Data Loss Prevention (or the file upload option will be greyed out) If your administrator has turned on data loss prevention this can also lead to the file upload option being greyed out as well.


2 Answers

you forgot to mention the enctype="multipart/form-data"

<form action="upload_handler.php" enctype="multipart/form-data" method="post">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>
like image 73
Yehia Awad Avatar answered Oct 20 '22 11:10

Yehia Awad


The issue is with the attributes in your <form> tag. To successfully enable files to be uploaded properly in HTML, following requirements should be there.

  • Make sure that the form uses method="post"

  • The form also needs the following attribute: enctype="multipart/form-data".

It specifies which content-type to use when submitting the form

So just add this enctype="multipart/form-data" part inside your <form> tag.

like image 39
Iresha Rubasinghe Avatar answered Oct 20 '22 11:10

Iresha Rubasinghe