I am writing a web application using CakePHP. Now with cake after a save has failed and is redirected back to an add or edit screen the data that was inserted is back in the form fields for the user to edit. This is exactly how I want it, the problem is the file upload field is not kept. Is there a way to retain the path for the file input form field when a user has selected an image to upload?
Thanks
Modern browsers will obfuscate the value of a file input to hide information about the client’s filesystem. This includes accessing the input value through javascript, where browsers will return something like
C:\fakepath\file.txt.Setting the value of file inputs is also generally not allowed, either by setting the
valueattribute in HTML, or through javascript. So, you would not be able to restore the previous value of the input field even if you could somehow find out what it was. Try for yourself here:http://jsfiddle.net/MP39Q/2/
(There are some exceptions to some of these restrictions, e.g. earlier versions of IE, but I imagine this doesn’t help you.)
Since the user’s path is not sent as part of the form data, it will not be available in PHP.
A solution in your case may be to keep the uploaded file on the server. When a save error occurs, pass an appropriate reference to the temporary file back to the form (e.g. through session data), and allow the user to choose whether to keep the original file, or select a new one to upload while they correct other parts of the form.