PDA

View Full Version : [PHP] Upload help



Moh
19-08-2008, 03:44 PM
Every time I upload an image, even if I leave the field blank or upload the right file, I always get the error for incorrect file type :S


elseif($_GET["act"] == "profilepic")
{
$max = "10000000";
$file = $_FILES ["file"] ["name"];
$type = $_FILES ["file"] ["type"];
$size = $_FILES ["file"] ["size"];
$tmpname = $_FILES ["file"] ["tmp_name"];
$info = getimagesize ( $tmpname );
$re1 = '(image)';

if($file != "" || $size < $max)
{
if (preg_match ( "/" . $re1 . "/is", image_type_to_mime_type ( $info [2] ) )) {

$tempname = $_FILES ["file"] ["tmp_name"];

$file_spaces = uniqid ( "img" ) . $_FILES ["file"] ["name"];
$name = str_replace ( " ", "_", $file_spaces );

move_uploaded_file ( $tempname, "/home/habbcr/public_html/djpanel/djpics/$name" );

mysql_query ( "UPDATE `users` SET `picture` = '$name' WHERE `username` = '$username'" );

notice("Profile Picture Updated!");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">Your new profile picture has now been uploaded. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
endnotice();
} else {
notice("Incorrect File Type");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">The file you tried uploading is <b>NOT</b> an image file. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");

}
}
else
{
notice("Error uploading file");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">There was an error uploading your file. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
}

So if the file is left blank, it is still getting past the first if :S

Moved by Invent (Forum Moderator) from Designing & Development: Please post in the correct forum next time, thanks :).

Dentafrice
19-08-2008, 03:47 PM
It's getting by because if the size is 0 (no file was uploaded..) it is less then the $max..

Source
19-08-2008, 03:50 PM
I think what you are looking for is to replace the

||

with

&&

I dunno really, because I think || mean or rather than and.

--ss--
19-08-2008, 03:55 PM
I think what you are looking for is to replace the

||

with

&&

I dunno really, because I think || mean or rather than and.
Yep, that should work as it's currently checking for if either one is true (The function for || ), the && is what you need to check if both match the criteria ;).

Moh
19-08-2008, 03:58 PM
It's getting by because if the size is 0 (no file was uploaded..) it is less then the $max..
Ah yes, but what about not letting me upload images if it meets the requirements?

I think what you are looking for is to replace the

||

with

&&

I dunno really, because I think || mean or rather than and.
|| Should work.

Dentafrice
19-08-2008, 03:59 PM
|| means or..

So it's literally saying..

If the file field is not blank or if the size is less then $max.

it should be

if the file filed is not blank AND (&&) if the size is less then $max

Moh
19-08-2008, 04:07 PM
Changed it to:


elseif($_GET["act"] == "profilepic")
{
$max = "10000000";
$file = $_FILES ["file"] ["name"];
$type = $_FILES ["file"] ["type"];
$size = $_FILES ["file"] ["size"];
$tmpname = $_FILES ["file"] ["tmp_name"];
$info = getimagesize ( $tmpname );
$re1 = '(image)';

if($file == "")
{
notice("Error uploading file");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">You did not select a file to upload. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
elseif($size > $max)
{
notice("Error uploading file");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">The file you selected is too big. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
else
{
if (preg_match ( "/" . $re1 . "/is", image_type_to_mime_type ( $info [2] ) )) {

$tempname = $_FILES ["file"] ["tmp_name"];

$file_spaces = uniqid ( "img" ) . $_FILES ["file"] ["name"];
$name = str_replace ( " ", "_", $file_spaces );

move_uploaded_file ( $tempname, "/home/habbcr/public_html/djpanel/djpics/$name" );

mysql_query ( "UPDATE `users` SET `picture` = '$name' WHERE `username` = '$username'" );

notice("Profile Picture Updated!");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">Your new profile picture has now been uploaded. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
endnotice();
} else {
notice("Incorrect File Type $tmpname");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">The file you tried uploading is <b>NOT</b> an image file. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
}
}

Now it can't get past:
if($file == "")

even if I have selected a file :S

Agnostic Bear
19-08-2008, 05:15 PM
Changed it to:


elseif($_GET["act"] == "profilepic")
{
$max = "10000000";
$file = $_FILES ["file"] ["name"];
$type = $_FILES ["file"] ["type"];
$size = $_FILES ["file"] ["size"];
$tmpname = $_FILES ["file"] ["tmp_name"];
$info = getimagesize ( $tmpname );
$re1 = '(image)';

if($file == "")
{
notice("Error uploading file");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">You did not select a file to upload. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
elseif($size > $max)
{
notice("Error uploading file");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">The file you selected is too big. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
else
{
if (preg_match ( "/" . $re1 . "/is", image_type_to_mime_type ( $info [2] ) )) {

$tempname = $_FILES ["file"] ["tmp_name"];

$file_spaces = uniqid ( "img" ) . $_FILES ["file"] ["name"];
$name = str_replace ( " ", "_", $file_spaces );

move_uploaded_file ( $tempname, "/home/habbcr/public_html/djpanel/djpics/$name" );

mysql_query ( "UPDATE `users` SET `picture` = '$name' WHERE `username` = '$username'" );

notice("Profile Picture Updated!");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">Your new profile picture has now been uploaded. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
endnotice();
} else {
notice("Incorrect File Type $tmpname");
echo("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"8;URL=?view=changedetails\">The file you tried uploading is <b>NOT</b> an image file. You are now being taken back to the \"change Your Details\" form<br /><br />If you'd prefer not to wait, please click <a href=\"?view=changedetails\">Here</a>");
}
}
}
Now it can't get past:
if($file == "")

even if I have selected a file :S

Sorry are you stupid or something, Caleb gave you (from what i can see after i skimmed it) the correct answer, use && instead of ||.

Moh
19-08-2008, 08:41 PM
Sorry are you stupid or something, Caleb gave you (from what i can see after i skimmed it) the correct answer, use && instead of ||.
When I changed it to && it still didnt work.

I'm surprised no one told me to look at the form, I noticed I didn't add enctype="multipart/form-data" to the form :eusa_wall

Excellent1
19-08-2008, 09:04 PM
When I changed it to && it still didnt work.

I'm surprised no one told me to look at the form, I noticed I didn't add enctype="multipart/form-data" to the form :eusa_wallI no no see form.

Want to hide these adverts? Register an account for free!