Forum Moderators: coopster

Message Too Old, No Replies

uploading a file and checking form fields, without reselecting file?

how to carry file location through form checking?

         

deejay

9:23 pm on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok. So I've got a page upload1.php which contains a form that asks the user to select a couple of variables:

File owner (compulsory)
Menu ID (compulsory)
File Description (optional)
File to upload

This form then goes to upload2.php where I check the form values, and if all ok write the info to my database and upload the file.

File owner and MenuID are compulsory, the first thing upload2.php does is check that these are completed. If either field is missing, the form is presented again for the user to complete the missing fields. Fields that were completed are carried forward into this form so the user doesn't have to re-enter.

the question: While I can carry the other fields forward from upload1.php to upload2.php, I can't figure out how or if I can carry forward the location of the file to be uploaded so the user doesnt' have to reselect?

Form from upload2.php follows. Area in question is highlighted - it's my best guess, but it doesn't work. Any suggestions?


<form enctype="multipart/form-data" action="upload2.php" method="POST">
File owner:
<?php
$res=mysql_query("select * from users ORDER BY lastname, firstname");
echo "<select name=\"user\" id=\"user\"><option value=\"\"></option>";
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option value=$row[userid]";
if($user==$row[userid])
echo " selected";
echo ">$row[lastname], $row[firstname]</option>";
}
echo "</select>";
?>

<p>Menu ID:
<?php
$res=mysql_query("select catmenu.menuid AS catmenuid, catmenu.menuname AS catmenuname, parentmenu.menuname AS parentmenuname from smenu AS catmenu LEFT JOIN smenu AS parentmenu ON catmenu.parentid = parentmenu.menuid WHERE catmenu.menuid>1 ORDER BY parentmenu.menuname, catmenu.menuname");
echo "<select name=\"menu\" id=\"menu\"><option value=\"\"></option>";
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option value=$row[catmenuid]";
if($menu==$row[catmenuid])
echo " selected";
echo ">$row[parentmenuname] - $row[catmenuname]</option>";
}
echo "</select>";
?>

<p>Please choose a file: <input name="uploaded" type="file" value="<?php echo $_FILES['uploaded']['name'];?>"/><br />

<p>Description of file: <input name="description" type="text" id="description" value="<?php echo $description;?>" style="height:78px;width:328px;">
<p><input type="submit" value="Upload" />
</form>

dreamcatcher

10:06 pm on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi deejay,

If the file has been uploaded successfully it will be in a temporary location on your server. Usually accessed in $_FILES['uploaded']['tmp_name'] on upload. You can then store this in a hidden var and then move/rename it to the new location afterwards:

<input type="hidden" name="file" value="<?php echo $_FILES['uploaded']['tmp_name'];?>" />

if (isset($_POST['file']))
{
rename($_FILES['uploaded']['tmp_name'],'files/file.jpg');
unlink($_FILES['uploaded']['tmp_name']);
}

Think that should work. This assumes the file is an image.

dc

deejay

1:19 am on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Dreamcatcher

Thanks for the heads up - think part of what I was missing was that I should have been looking for the temp name rather than the original filename.

No, the files aren't necessarily jpgs, or images for that matter - I've an arm-long list of file types I want to allow, but I think I'm getting somewhere with it now, thanks to your help.

Once I get it all nutted out I'll come back and post the complete code - it's been a fun exercise and might be useful to someone else.. heh.. and maybe I can get some touch-up ideas.