PDA

View Full Version : How to check if image was selected/added in form ?



darekw1
04-12-2008, 06:05 AM
Need help with checking if picture was uploaded to var imagefile or not.
Here is the form to upload image:
<form enctype="multipart/form-data" action="Insertion_Request.php" method="post">
.....input text
.....input text
....input text
and last field is for image......
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
<input type=file name=imagefile >
<input name="submit" type=submit value='Submit Form'>

Then in page Insertion_Request.php checks if imagefile has data and if so loads it to Mysql table.

As you can see Author of this code sets variable completed to 1 and then checks in Insertion_Request.php if completed = 1 which suppose to mean that image was uploaded to var imagefile:

if if ($_REQUEST[completed] == 1) { Insert imagefile to Mysql......}

However var completed is always set to 1 when form is submitted, no matter if image was selected or not.

I have tried to check if imagefile has data like this:

$Picture=$_POST['imagefile'];

if (!$Picture) { then ...}

but it does not work.
So how can I check if user added image or submitted form without selecting image?

linFox
04-12-2008, 09:11 AM
Data about the files that are uploaded to PHP is not stored in the $_POST superglobal; rather $_FILES (each file uploaded being assigned $_FILES['htmlfieldname']). In this case, the data is contained in the array $_FILES['imageFile'].

You can see all the related data yourself by using the line print_r($_FILES['imageFile']); to print the full array, but the basic items in the array are:
name, the original filename of the upload (ie. myuploadedfile.jpg);
tmp_name, the fully-qualified path to the uploaded file on the server (in the temp. directory with a temp. name);
size, the size in bytes of the uploaded file;
error, the PHP upload error flag;
type, the MIME type of the file reported by the browser.

To check if the file has been uploaded, check the file upload error flag.
A value of 0 means the file was uploaded successfully; a value of 4 means no file was uploaded (the other values relate to various other more serious errors, you can see a full list here (http://php.net/manual/en/features.file-upload.errors.php)).

So, to do a simple check to see if a file was not uploaded, (I seriously recommend other integrity checks, at least file extension or content) use:
if ($_FILES['imageFile']['error'] != 0) { then not uploaded or upload error...}

shadmego
04-12-2008, 12:45 PM
Moderators are always deleting posts because of spam, telling people to relax and generally "keeping the peace".

Very rarely do we compliment members for their outstanding support and help they provide for community members.

I would like to change that. We don't have awards to give out for outstanding posts so this is nothing concrete, but I would like to highlight linFox's response to this thread.

It is an example of the type of answer we should all strive to provide other members here.

I've recently heard some bad things about the Joomla! forum and the phpbb forum. While the numbers don't match, it is a shame people are having terrible experiences there.

On the other hand, I like that people are coming here for help when they can't get it elsewhere.

Thank you, linFox for your continued, professional support of the Hostmonster user community. It is because of people like you that we continue to grow our membership.

~regards

linFox
04-12-2008, 08:22 PM
Wow, thanks again shadmego. :o

darekw1
04-16-2008, 06:47 AM
Thank you from me too !

you advise worked beautifully !
I'm very very happy to be with you guys. This is by far the most proffesional webhost I have experience so far and I try to tell everyone about you !

Darius