50

Basically, I have this form that allows user to upload to my server:

<form id = "uploadbanner" method = "post" action = "#">
      <input id = "fileupload" type = "file" />
      <input type = "submit" value = "submit" id = "submit" />
</form>

But the problem is that when I choose a file, then click submit, I don't see the file uploaded in the server directory.

Meloman
  • 3,558
  • 3
  • 41
  • 51
dave
  • 14,991
  • 26
  • 76
  • 110
  • 7
    HTML can only select and send the file content. It's the server's responsibility to get the file content. HTML doesn't run in the server. Only server side languages like Java/C#/PHP/JSP/ASP/etc runs on the server side. Yet you didn't mention which one you're using. The answer depends on that. So please mention and tag which one you're using. Based on your question history it's PHP. Is this true? If so, then this question is a dupe of http://stackoverflow.com/questions/198346/whats-the-best-way-to-create-a-single-file-upload-form-using-php – BalusC Apr 11 '11 at 21:59
  • 5
    What is your backend language? You cannot just put HTML on a page and expect your web browser to upload things to your computer. That would be a huge security flaw. – Devin Burke Apr 11 '11 at 21:59

4 Answers4

76
<form id="uploadbanner" enctype="multipart/form-data" method="post" action="#">
   <input id="fileupload" name="myfile" type="file" />
   <input type="submit" value="submit" id="submit" />
</form>

To upload a file, it is essential to set enctype="multipart/form-data" on your form

You need that form type and then some php to process the file :)

You should probably check out Uploadify if you want something very customisable out of the box.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
Calum
  • 5,308
  • 1
  • 22
  • 27
22

You need enctype="multipart/form-data" otherwise you will load only the file name and not the data.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
Darren
  • 548
  • 7
  • 10
  • 13
    There is no need to echo other comments. The answer was already edited to contain this. – Mike Oct 13 '16 at 14:47
  • 1
    No, because "otherwise you will load only the file name" is a really helpful information, so this answer is significant. – Hartmut Pfarr Apr 24 '22 at 14:14
1

On top of what the others have already stated, some sort of server-side scripting is necessary in order for the server to read and save the file.

Using PHP might be a good choice, but you're free to use any server-side scripting language. http://www.w3schools.com/php/php_file_upload.asp may be of use on that end.

M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
Dane
  • 9,242
  • 5
  • 33
  • 56
0

you will need to have a backend on the server do handle the file upload request you can base your backend with this php example

<?php
$file = $_FILES['file'];
move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']);?>

Then point the form to the file

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="/tourscript.php">
   <input id="fileupload" name="myfile" type="file" />
   <input type="submit" value="submit" id="submit" />
</form>