Forum Moderators: coopster

Message Too Old, No Replies

Image Upload Problem

         

compose

10:37 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



Hi,

I am submitting a form with one image upload option. Now problem is that if a user submit form without choosing any image my script is displaying a blank page and not insert data in db. But when i select image with submition of formit is working fine.

I think problem is i have to check whether file is selected or not. I am using like this

if(isset($_POST['submit']))
{
if(isset($_POST['userfile']))
{
echo "image selected";

if (isset($_FILES['userfile']))
{
//code to uplaod image
}
}
else
{
echo "no image selected";
}
}
[/code]

when i use this it always falls in else condition that no image selected whether i choose image or not.

can any body help me what is problem inthis.

Vinic

coopster

10:56 pm on Feb 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if(isset($_POST['submit'])) { 
if(isset($_POST['userfile'])) {
echo "image selected";
if (isset($_FILES['userfile'])) {
//code to uplaod image
}
} else {
echo "no image selected";
}
}

It's your second line here, notice you are checking the $_POST superglobal and not the $_FILES superglobal. That is why you are failing every time. You seem to be trying to do too much here ... Just check for the existence of the $_FILES value instead.
if (isset($_FILES['userfile']) && trim($_FILES['userfile'])) { 
//code to uplaod image
} else {
echo "no image selected";
}

compose

11:31 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



Hi,

Thanx for your fast reply.I check your given code after some modifications it is running fine. thanx

if (isset($_FILES['userfile']) && trim($_FILES['userfile']))

Alteration
-----------
if (isset($_FILES['userfile']) && trim((string)$_FILES['userfile']['name']))

Thanx again...

Regards,
Vineet

jatar_k

12:02 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why do you need to typecast it?

coopster

12:03 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome. Yeah, the additional '&&' was an afterthought that I edited in quickly, nice catch ;-)

<added>
Really don't need to typecast, jatar_k is correct ;)
</added>

compose

12:19 am on Feb 19, 2006 (gmt 0)

10+ Year Member



Hi,

I typecast it because when i run this code it was giving error that trying to convert array value to string.

Vineet

coopster

12:30 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm guessing that was before you added the final ['name'] index though. You really shouldn't need it now.