Page 1 of 1
Is it possible to upload an image to a server via form?
Posted: Thu May 19, 2005 1:14 pm
by slipstream
Hi there,
I am creating a form where users fill in fields in order to add products to a database. The issue now is I want them to be able to browse their hard drive and select an image to upload to the server. The image can be placed in a directory but I want to be able to overwrite images that exist in that folder on the server. I need it simple for the user to be able to upload.
Any ideas?
Posted: Thu May 19, 2005 1:17 pm
by shiznatix
yup very possible
use getimagesize to make sure its a image
do a if file exists then delete file then do the uploading of the new file so it will overwrite the file else just upload the file
write all the info to the database
shouldnt be too hard. if you have any trouble post some code
Posted: Thu May 19, 2005 1:20 pm
by slipstream
Thanks,
Is there a function that opens the users HD window so they can select an image?
Posted: Thu May 19, 2005 1:23 pm
by shiznatix
http://ee.php.net/features.file-upload
yup php definatly supports file uploads and whatnot. read that and it even gives you a same code to use to upload the file then, like i said before, use getimagesize to verify that is a file so someone doesnt try pulling a fast one on you and uploading a file that is called virus.exe.jpg. getimagesize would return false on that but a file extension grabber would return true.
Posted: Thu May 19, 2005 1:42 pm
by slipstream
Ok I seem to have got it but I have a user concern I am wondering about. The form has many fields and by adding this upload feature to it, it makes that part of the error checking now. If the user messes up another part of the form, it gives an error and they will have to re-select each image (there are 3). I am worried about it getting a bit annoying for the user if they make an error on another part of the form and have to re-select all the images.
Any ideas on that?
Posted: Thu May 19, 2005 1:46 pm
by shiznatix
you could at the top of the processing page set the 3 pathes to the files to seperate session variables ie
Code: Select all
$_SESSION[file_a] = //whatever;
$_SESSION[file_b] = //whatever;
$_SESSION[file_c] = //whatever;
then if it messes up (ie the user chooses a incorrect file) you can use
Code: Select all
<input name="e;userfile"e; type="e;file"e; value="e;<? echo $_SESSIONїfile_a]; ?>"e;>
do that for all 3 and i guess that would work. thats completly untested but i assume it would work.
Posted: Thu May 19, 2005 5:52 pm
by onion2k
shiznatix wrote:thats completly untested but i assume it would work.
It won't. You can't populate a file upload element from the server. It'd be a huge security risk.
Posted: Thu May 19, 2005 5:57 pm
by Burrito
Nope, onion is right...can't do it.
you could check to make sure the form isn't "messed" up on the client side with JS and then it would keep the file info an just tell them to fix what's "messed" up.
those who dont' have JS enabled would suffer, but tough for them for not catching up to the 21st century.