PhilAJ Posted December 15, 2012 Share Posted December 15, 2012 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 More sharing options...
davej Posted December 16, 2012 Share Posted December 16, 2012 (edited) 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 December 16, 2012 by davej Link to comment Share on other sites More sharing options...
PhilAJ Posted December 16, 2012 Author Share Posted December 16, 2012 Thanks for reply.... ...So any idea how do I refresh the page after errors without forcing the user to have to broowse again... Phil Link to comment Share on other sites More sharing options...
davej Posted December 17, 2012 Share Posted December 17, 2012 Well, you can save the file name and you can save the file contents. I just don't know how to save the file handle since knowledge of the path is apparently forbidden. Link to comment Share on other sites More sharing options...
PhilAJ Posted December 18, 2012 Author Share Posted December 18, 2012 Thanks for the reply - but HOW do you save the filename - what is it held in? Link to comment Share on other sites More sharing options...
thescientist Posted December 18, 2012 Share Posted December 18, 2012 the $_FILES array. you can find more information in the tutorials.http://www.w3schools.com/php/php_file_upload.asp Link to comment Share on other sites More sharing options...
davej Posted December 18, 2012 Share Posted December 18, 2012 (edited) In Javascript it is held in the element.files[0].name. Edited December 18, 2012 by davej Link to comment Share on other sites More sharing options...
PhilAJ Posted December 18, 2012 Author Share Posted December 18, 2012 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 More sharing options...
thescientist Posted December 18, 2012 Share Posted December 18, 2012 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 More sharing options...
PhilAJ Posted December 20, 2012 Author Share Posted December 20, 2012 Didnt try Daves - tried the othe one and it just gave the filename not the path - I'll try the javascript one... Link to comment Share on other sites More sharing options...
davej Posted December 20, 2012 Share Posted December 20, 2012 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 More sharing options...
PhilAJ Posted December 20, 2012 Author Share Posted December 20, 2012 I've been trawling thru loads of forums and there appears to be no way to do this in IE. Only way may be to submit to server - save as a temp file and redisplay - then when confirmed save for real..... thanks for help Link to comment Share on other sites More sharing options...
davej Posted December 23, 2012 Share Posted December 23, 2012 (edited) 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 December 23, 2012 by davej Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now