Forum Moderators: coopster

Message Too Old, No Replies

Image Upload Issue

problems uploading images with php

         

bysonary

10:43 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



Hello everybody, hope your all good.

I am trying to upload images only to a directory on my web server with very little success, here is the code i have so far.


<?php
if ($userfile_size >3500000)
{
$msg=$msg."Your uploaded file size is more than 3500KB, it needs to be smaller!";
$file_upload="false";
}


if (!($userfile_type =="image/pjpeg" ¦¦ $userfile_type=="image/gif" ¦¦ $userfile_type=="image/png" ¦¦$userfile_type=="image/bmp"))
{
$msg=$msg."Wrong File Type";
$file_upload="false";
}


$add = "/home/www/juttuffi/gallery/images/$userfile_name";
if(move_uploaded_file ($userfile, $add))
{
echo "UPLOADED";
}
else
{
echo "Failed to upload file ";
}
?>

Basically I get the error "Failed to upload file" so i guess the problem is related to the move_uploaded_file()

can anyone drop me any hints as to how to fix this problem, Please please please!

henry0

11:37 pm on Jan 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First I'll do that:
// Get the details of "imagefile"

$filename = $_FILES['imagefile']['name']; //echo"$filename";
if (!eregi(".jpg¦.png¦.jpeg¦.pjpeg]",$filename))
{
echo"<b>Unauthorized file format!<br>
<a href=\"../upload.php\"> Please try again!</a></b>";
exit();
}
else
{
// carry on
}

then I will change $add
to something like
../../gallery etc...

bysonary

11:40 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



I dont understand, you mean add this code at the top of my code and remove the mime type check in my code?

bysonary

12:12 am on Jan 30, 2007 (gmt 0)

10+ Year Member



if ($_FILES['userfile']['type']!= 'image/gif' ¦¦ 'image/jpeg' ¦¦ 'image/bmp' ¦¦ 'image/png')

the above code doesnt work, can anyone help?

it comes uo saying file not an image, is that check correct? are the OR ¦¦'s correct? can anyone help?

kappaknight

4:10 am on Jan 30, 2007 (gmt 0)

10+ Year Member



Sorry to hijack the thread but I'm having problems with the same issue trying to upload files to a Windows 2003 server. I think I have my permissions set right but it's just not uploading...

bysonary

12:01 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



Hello again

I can get files to upload fine when I have this


// Does the file have the right MIME type?
if ($_FILES['userfile']['type']!= 'image/gif')

The obvious solution to me is to add an OR so like,

gif OR jpeg OR bmp OR png surely that would be the way I would go about it in my head, i suppose i could do it like.


if ($_FILES['userfile']['type']!= 'image/gif')
{
//ERROR CODE
}
elseif ($_FILES['userfile']['type']!= 'image/jpeg')
{
//ERROR CODE
}

and keep going like that, can someone please please help I am so close it uploads GIF files fine i just need it to accept jpeg and png and bmp files too

are my ideas about the OR and elseif right? or am i being stupid (I am a newbie to php, but not programing)

if someone could help me get it working I would be really grateful

Scally_Ally

1:34 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



What henry0 is saying is to check the file name string to see if it contains the file strings you are looking for.
This could be used as a replacement for the IF / OR statement you have going on.
Check to see if this is where you are getting the error still by maybe echoing and exiting at certain points.
If it still aint working then post your revised code.
Ally

cmarshall

1:41 pm on Jan 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just so you guys know. I was having an issue where the file uploaded, but disappeared. It never showed up in the $_FILES superglobal.

The problem was with the permission of the destination directory. Apparently, PHP does not use the same user as Apache. Rather than go through the rigamarole of finding the PHP user, I made the directory 0777 with an .htaccess to prevent HTTP and PHP execution. Since anyone who got to this point would be in the system hip-deep anyway, it wouldn't matter. However, for a more publicly available system, I would track down the PHP user and set chown to that.

CORRECTION: It DID show up in $_FILES. move_uploaded_file() just quietly failed.

bysonary

2:50 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



Yes but I just want to know why on earth my IF OR thing isnt working as it should and if it should work? I even tried elseif for each mime type and that didn't work.

Where am i going wrong, Shouldn't what I want to do and the way im trying with If and OR work?

just to make it clear to everyone here is the code I currently have which doesn't work however it does if the OR isnt in the MIME type section is removed and it only checks one type of file.


<?php
if ($_FILES['userfile']['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}


// Does the file have the right MIME type?
if ($_FILES['userfile']['type']!= 'image/jpeg' ¦¦ 'image/gif')
{
echo 'Problem: file is not an image';
exit;
}


// put the file where we'd like it
$upfile = '/home/www/juttuffi/gallery/images/'.$_FILES['userfile']['name'];


if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['userfile']['name'];
exit;
}


echo 'File uploaded successfully<br><br>';
?>

I have also tried the following..


<?php
if ($_FILES['userfile']['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}


// Does the file have the right MIME type?
if ($_FILES['userfile']['type']!= 'image/jpeg')
{
echo 'Problem: file is not an image';
exit;
}
elseif ($_FILES['userfile']['type']!= 'image/gif')
{
echo 'Problem: file is not an image';
exit;
}


// put the file where we'd like it
$upfile = '/home/www/juttuffi/gallery/images/'.$_FILES['userfile']['name'];


if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['userfile']['name'];
exit;
}


echo 'File uploaded successfully<br><br>';
?>

both different approaches dont work and i cant figure out why, does it have something to do with exit;

?

cmarshall

3:12 pm on Jan 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ($_FILES['userfile']['type']!= 'image/jpeg' ¦¦ 'image/gif')

I didn't know PHP allowed this. I always compare like so:

if ($_FILES['userfile']['type']!= 'image/jpeg' ¦¦ $_FILES['userfile']['type']!= 'image/gif')

Scally_Ally

3:42 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



on your else if statement in the second example


// Does the file have the right MIME type?
if ($_FILES['userfile']['type']!= 'image/jpeg')
{
echo 'Problem: file is not an image';
exit;
}
elseif ($_FILES['userfile']['type']!= 'image/gif')
{
echo 'Problem: file is not an image';
exit;
}

it will not work properly because if the file type is a gif it will get caught by the first if statement, as it is not a jpeg.
Just do 1 if statement to handle all file types, but flip it aswell so instead of it checking to see if it isnt a particular file type, check to see if it is.

//----------------------
if ($_FILES['userfile']['type'] == 'image/gif' ¦¦ _FILES['userfile']['type']== 'image/jpeg'){

//then do upload code here

}
else{

echo "this aint an image";

}

Ally

bysonary

3:43 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



I tried that and it gives and unexpected T_STRING on line 22 which is the line that contains the if statement...

if ($_FILES['userfile']['type']!= 'image/jpeg' ¦¦ $_FILES['userfile']['type']!= 'image/gif')

bysonary

3:58 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



god this thing just doesn't wanna work for me,

I took allys advice and changed the check so my code now looks like this


<?php
if ($_FILES['userfile']['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}


// Does the file have the right MIME type?
if ($_FILES['userfile']['type'] == 'image/gif' ¦¦ $_FILES['userfile']['type']== 'image/jpeg')
{
// put the file where we'd like it
$upfile = '/home/www/juttuffi/gallery/images/'.$_FILES['userfile']['name'];

//uploaded file
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['userfile']['name'];
exit;
}
}
else
{
echo 'Problem: file is not an image';
exit;
}
echo 'File uploaded successfully<br><br>';
?>

I assume this looks right to everyone? but why am I still getting an error on line 22 for an unexpected T_STRING in line 22 which is this line...


if ($_FILES['userfile']['type'] == 'image/gif' ¦¦ $_FILES['userfile']['type']== 'image/jpeg')

can anyone see the problem?

Scally_Ally

4:17 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



the only thing i can see is that

¦¦

is not the same as

¦¦

in the if statement

if ($_FILES['userfile']['type'] == 'image/gif' ¦¦ $_FILES['userfile']['type']== 'image/jpeg')

!SORRY that is completely wrong.. just my editor showing it differently..!

[edited by: Scally_Ally at 4:18 pm (utc) on Jan. 30, 2007]

jatar_k

6:00 pm on Jan 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



remember the WebmasterWorld software breaks pipe characters, those pipes with the space in them won't work

you need to replace them with a true pipe character

Stryker

6:05 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



What image file are you testing it with?

Different browsers have different definition of mime codes for jpg images.

Let me yank out an example from my code library here...

if ($_FILES['picture']['type'] == "image/pjpeg" ¦¦
$_FILES['picture']['type'] == "image/jpg" ¦¦
$_FILES['picture']['type'] == "image/jpeg")
{
//jpg code
}
....

That should catch all images of type jpg in IE, Firefox, and Opera.

bysonary

6:10 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



I never realized that, It now works perfectly, Or it seems to I will test it

just tested it and it works fine now phew thanks alot everyone :-) i can't say it enough!