Jump to content

HTML 'FILE' input type - get filename value


PhilAJ

Recommended Posts

Hope someone can help I have a page containing a form for entering details and I also want the user to be able to upload a Graphic file. I have a FILE input type, which allows the user to BROWSE to their local PC - the filename is then displayed - and the file uploaded. All works fine. However - if they enter incorrect data into one of the other fields, (which are validated in PHP and then page redisplayed), when the page is redisplayed it does not have the filename that was browsed to in the FILE field. SO - when data corrected and resubmitted no file is uploaded, unless they browse again. The question is - where is this filename held and how can I get it to be there in the FILE field - lilke I do with other data fields with a php echo. I have tried to use a Value= but that doesn't appear to work. Help appreciated Phil

Link to comment
Share on other sites

Good question. The path is hidden so I guess the only thing you could remember would be the element or the element.files[0] object. Does that act as a file handle?I have a similar issue with the color picker -- and in that case there shouldn't be any browser security confusion.

Edited by davej
Link to comment
Share on other sites

Thanks for that guys - and the [FILE]["name"] does indeed give the filename - but NOT the full path of where it came from. Maybe I have my logic wrong somewhere - but if I explain what is happening maybe somepne could help; Page displayedUser enters text into fieldsUser Browses for a graphic and selects - full pathname is shown in the browse fieldHit Submit PHP validates the data entered - finds an error in something in the data (eg invalid email address etc)Redisplays the page with data as entered and errors highlighted - except that the filename browsed for ISN'T thereUser Corrects the errorsUser Re submits - NO FILE uploaded as the path isnt present... Any suggestions for ho wthis should be done? Phil

Link to comment
Share on other sites

I guess at that point I would try validating on the client side first before the user submits. (and still validate on the server side). What about DaveJ's suggestion though? Did you try to output that value after a user has uploaded a file?

Link to comment
Share on other sites

No, that is the issue. For security reasons the path is unavailable. In fact it will give you a string something like "fakepath/yourfilename" that actually says "fakepath" so there must be a way to get a file handle, unless they expect you to store the entire file contents.

Link to comment
Share on other sites

The browser knows the path to the file. It just isn't allowed to reveal it. I would have thought that the correct way to deal with that would be to have a file handle. If they didn't provide a file handle then I guess you have only one opportunity to read the file -- unless perhaps you use AJAX so that you don't reload the page.

Edited by davej
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...