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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

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

Post 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 :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

It'd make more sense to me to give the image and its information at the same time.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

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

Post 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.
Post Reply