Page 1 of 1

How to remember input value for type=file on post?

Posted: Fri Jan 23, 2009 3:23 pm
by ben.artiss
Hi,

I was wondering if anyone could help me on this one? I'm trying to make an image gallery admin area in PHP and I'm trying to add a system kind of like the phpMyAdmin interface i.e. when adding a table you can add more rows- and when you add a new row all the <input>'s values are remembered. I know how to do this using every form element except for type='file'! Just to clarify:

Code: Select all

<?php
$error = false;
 
if (isset($_POST['upload'])) {
 if (!trim($_POST['title'])) {
  $error = true;
 }
 
 if (!$error) {
  # finish the job
 }
}
?>
<html>
...
<form method="post" action="" enctype="multipart/form-data">
 <input type="text" name="title" <?php if ($error) {echo 'value="'.htmlspecialchars($_POST['title']).'"';} ?> /><br />
 <input type="file" name="image" <?php if ($error) {echo "HOW DO YOU GET THE USER INPUT BACK?";} ?> />
 <input type="submit" name="upload" value="Upload" />
</form>
...
</html>
Any help would really be appreciated! I have a feeling it might not be possible because of something to do with security, but if it is it would save me looking into java!

Thanks in advance

Re: How to remember input value for type=file on post?

Posted: Fri Jan 23, 2009 3:42 pm
by requinix
ben.artiss wrote:I know how to do this using every form element except for type='file'!
Can't be done.
In your shoes I would turn the field into a readonly textbox with the name of the file that was uploaded and put a "Delete?" checkbox next to it.

But you can dynamically add stuff using JavaScript - and the form doesn't have to be submitted to do it.

Re: How to remember input value for type=file on post?

Posted: Fri Jan 23, 2009 4:04 pm
by ben.artiss
Do you think it would be better to get the user to select the pics before going to a separate page to enter the information about each image? If so - do you know if it's possible to validate a file array (i.e. filesize/type) using ajax (backed up by php validation later)? Thanks for your reply :)

Re: How to remember input value for type=file on post?

Posted: Fri Jan 23, 2009 4:37 pm
by requinix
It'd make more sense to me to give the image and its information at the same time.

Re: How to remember input value for type=file on post?

Posted: Fri Jan 23, 2009 5:30 pm
by ben.artiss
You've got a good point there - how much can really go wrong with a title and description lol, thanks for your help man.