Is it possible to upload an image to a server via form?

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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Is it possible to upload an image to a server via form?

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

Thanks,

Is there a function that opens the users HD window so they can select an image?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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=&quote;userfile&quote; type=&quote;file&quote; value=&quote;<? echo $_SESSIONїfile_a]; ?>&quote;>
do that for all 3 and i guess that would work. thats completly untested but i assume it would work.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

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