3

Ok I really didn't know how else to sum it up for the title. I need to create a form where a user can specify an existing file, but I don't need to upload it (it's on a shared server already accessible) -- I just need to grab the filename and save it in the database so it's linked to other data.

I thought about the input tag as it provides a convenient already done interface to the user's filesystem, but I don't know if it's possible to use it without the acutal upload (leaving out enctype="multipart/form-data" doesn't seem to work). Can I tweak this to make it work, or is there any way to do it other than writing a custom mini file browser?

EDIT: I need the full path name of the file, asking the users to enter it is out of the question...

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
  • 1
    You will not get the full path name, as this is irrelevant from the web servers point of view. – Frank Bollack Feb 01 '10 at 10:07
  • You can only get the filename, not full path. See http://stackoverflow.com/questions/2027276/show-selected-file-without-postback/2027301#2027301 – Tatu Ulmanen Feb 01 '10 at 10:12
  • I'm also fairly certain that the HTML spec doesn't even guarantee you'll even get a filename, though most browsers will pass it in. – Jon Feb 01 '10 at 20:13

1 Answers1

6

You could place the <input type="file"> outside your HTML form, and then use the onChange event to fill an <input type="hidden"> within the form that gets posted:

<input type="file" 
       onchange="document.getElementById('hidden_file').value = this.value;" />

<form method="POST">
    <input type="hidden" id="hidden_file" value="" />
    <input type="submit" />
</form>
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • This is brilliant, but is there really no way of retrieving the full path too? – Matteo Riva Feb 01 '10 at 10:12
  • 1
    Unfortunately: http://stackoverflow.com/questions/1676035/cant-get-the-complete-address-while-uploading-a-file, but you may want to investigate further: http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3 – Daniel Vassallo Feb 01 '10 at 10:13