File Feilds, PHP and Javascript... (maybe)

admin

Administrator
Staff member
I am developing a backend for an art gallery style website in PHP/MySQL. I have a form and it has some fields (duh! :) ) including a "FILE" field. When the form is submitted my PHP code checks for errors, such as no Artist Name. If there is an error the data entry page is shown again with the warning. The fields are filled with the variables that were submitted so that the user doesn't have to fill everything out again. I have managed to "fill" all of the fields except the "FILE" one...<br />
<br />
I don't know much about "FILE" fields but I don't think that you can give them a defult value because you shouldn't know what is on the user's machine... is this true? And if it is, is there any way to MAKE it take the value of the variable? I tried this...<br />
<br />
<input type=file name="upload" value="<?php echo "$upload"; ?>"><br />
<br />
and that doesn't work...<br />
<br />
I would really prefer it if the user does not have to select the file to upload again...<!--content-->Yes that's right, it's a security thing. It is valid HTML, but it's up to the browser whether you can set the default value. I don't think there is anything you can do about it I'm afraid.<!--content-->I Think so too, you can't fill in a default value<!--content-->From my understanding when I submit a form with a "file" field in it, the fille is uploaded to the server's "tmp" directory no matter what? If this is true, will it stay there for a few minutes or so if it isn't used or unlinked? And if so can I store the temporary name in a vairable and use the temp location in a script later to upload the file.... this way the user will not have to choose again?<br />
<br />
If so could somebody tell me the variable that tells me the temp name?<!--content-->For example:<br />
<br />
<html><br />
<head></head><br />
<body><br />
<?PHP<br />
if(!empty($file_name))<br />
{ if(copy($file,"C:/tmp/$file_name")) // tmp dir<br />
{ if(is_uploaded_file($file))<br />
{ echo "<b>Upload is done!</b><br>";<br />
echo "File-Name: $file_name";<br />
echo "<br>";<br />
echo "File-Temp-Name: $file";<br />
echo "<br>";<br />
echo "File Size: $file_size Byte";<br />
}<br />
}<br />
}<br />
else<br />
{<br />
?><br />
<br />
<form name="form1" enctype="multipart/form-data" method="post" action="test.php"><br />
<p> <input type="file" name="file"><br><br />
<input type="submit" value="Upload"></p><br />
</form><br />
</body><br />
<?PHP<br />
}<br />
?><!--content-->While it appears that you cannot set the value of the FileUpload field, you can retrieve it. I'm not sure if you can see the value from php, but I've tried it with JavaScript, it works. If you can see it using php, great.<br />
<br />
I would provide a text box somewhere near the FileUpload, saying "This is the path you used last", where your php fills in the file name. You can get the user to copy/paste.<br />
How do you get the filename in the first place? I did it with JavaScript: onSubmit I copied the value of the FileUpload to a hidden control and this got submitted (in case if you can't do it with php). But JavaScript may be disabled, etc. Use php if you can.<!--content-->Originally posted by King Pellinore <br />
You can get the user to copy/paste.<br />
How do you get the filename in the first place? <br />
<br />
I am sure that you can't simply copy & paste the information into a "file" field... even if it let you the file field must store other information than what is displayed in the little box - only the file name is displayed and not the path on the users machine.<br />
<br />
I think that i have to give up on this... it seems that the only real solution is to save it to the server on the chance that the user completes the form correctly or just make them select the images again. I guess it is their own fault if they can't fill the form correctly, and having to select the images again is their minimal punishment :) <br />
<br />
Thanks everyone for your help!<!--content-->hmm... The whole path displays on my machine (thats win98, ie6) and i can copy/paste into the file box, but maybe the other people can't? I don't know.<br />
<br />
The punishment idea is good.<!--content-->So your "file" field allows you to copy and paste and it displays something like <br />
"C:/My/File/To/Upload/My-File-To-Upload.txt" ?<br />
<br />
I am using IE 5.2 on Mac OS X<br />
<br />
Interesting :)<!--content-->
 
Back
Top