Author Topic: cant check file extension correctly  (Read 1504 times)

0 Members and 1 Guest are viewing this topic.

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
cant check file extension correctly
« on: November 09, 2006, 05:51:51 PM »
I get this error-------

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in /nfs/cust/6/85/82/728586/web/upload.php on line 67
Not a JPEG file: starts with 0x47 0x49
Warning: imagecreatefromjpeg(): 'imgs/o1163112384.jpg' is not a valid JPEG file in /nfs/cust/6/85/82/728586/web/upload.php on line 67

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /nfs/cust/6/85/82/728586/web/upload.php on line 69
Error Re-sampling image

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in /nfs/cust/6/85/82/728586/web/upload.php on line 102
Not a JPEG file: starts with 0x47 0x49
Warning: imagecreatefromjpeg(): 'imgs/o1163112384.jpg' is not a valid JPEG file in /nfs/cust/6/85/82/728586/web/upload.php on line 102

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /nfs/cust/6/85/82/728586/web/upload.php on line 104
Error Re-sampling image

Warning: imagedestroy(): supplied argument is not a valid Image resource in /nfs/cust/6/85/82/728586/web/upload.php on line 117


But i feel its because my file checker doesnt work.........

here is my script------


http://pastebin.com/820708

my image checker is on line 28-29 and i want to make it only allow .jpeg or .jpg images
« Last Edit: November 09, 2006, 05:54:25 PM by allaboutthekick »

Offline brendandonhue

  • Enthusiast
  • Posts: 70
    • View Profile
Re: cant check file extension correctly
« Reply #1 on: November 09, 2006, 07:23:10 PM »
You can use the results of getimagesize() to tell if the file is actually a JPEG.

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #2 on: November 09, 2006, 07:44:37 PM »
i dont know too much about php how would i incorporate that into the script? is it a quick add or do i need to re arrange some stuff. I didnt write this script i just downloaded it and altered it a little. 

Offline brendandonhue

  • Enthusiast
  • Posts: 70
    • View Profile
Re: cant check file extension correctly
« Reply #3 on: November 09, 2006, 08:02:10 PM »
Change this:
list($width_orig, $height_orig) = getimagesize($add);
To this:
list($width_orig, $height_orig, $type) = getimagesize($add);

Then $type will have a value of 2 if you're working with a JPEG.

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #4 on: November 11, 2006, 01:31:41 AM »
how would i make it stop the script and display an error once the value of $type doesnt equal 2.

Offline heckenschutze

  • Enthusiast
  • Posts: 444
  • Gender: Male
    • View Profile
Re: cant check file extension correctly
« Reply #5 on: November 11, 2006, 01:48:37 AM »
by using:

Code: [Select]
die("Error not a jpeg");
or

Code: [Select]
exit();

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #6 on: November 11, 2006, 02:00:34 AM »
so something like

if(!$type == 2);
die("Error not a jpeg");



and
how would i make it delete the uploaded file?


Offline heckenschutze

  • Enthusiast
  • Posts: 444
  • Gender: Male
    • View Profile
Re: cant check file extension correctly
« Reply #7 on: November 11, 2006, 02:06:14 AM »
More like:

Code: [Select]
if($type != 2)
    die("Error not a jpeg");

To delete a file, use
Quote
bool unlink ( string filename [, resource context] )

Example:
Code: [Select]
if(unlink($strFilename) == TRUE)
{
    echo "deleted file";
}else{
    echo "failed to delete file";
}

hth :)

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #8 on: November 11, 2006, 03:32:11 AM »
ok so this is what i did right here

Code: [Select]
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
 
$userfile=$_FILES['userfile']['tmp_name'];
$userfile_size=$_FILES['userfile']['size'];
$userfile_type=$_FILES['userfile']['type'];
$userfile_name=$_FILES['userfile']['name'];
 
list($type) = getimagesize($userfile);

if($type != 2){
    die("Error not a jpeg");

 but it always displays "error not jpeg" even when it is a jpeg.......

sooooo close!!!!
« Last Edit: November 11, 2006, 04:29:30 AM by allaboutthekick »

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #9 on: November 11, 2006, 05:24:45 AM »
i did some searching and found this command

Code: [Select]
exif_imagetype()
http://us3.php.net/manual/en/function.exif-imagetype.php


and this


Code: [Select]
image_type_to_extensionhttp://us3.php.net/manual/en/function.image-type-to-extension.php

i feel that one of those codes is the one i should use but i do not know how to implement them....
« Last Edit: November 11, 2006, 05:29:30 AM by allaboutthekick »

Offline Jaysonic

  • Fanatic
  • Posts: 3,332
  • Gender: Male
  • 8548863
    • View Profile
    • I Am Lewy Babes
Re: cant check file extension correctly
« Reply #10 on: November 11, 2006, 06:08:14 AM »
when using the list function you must do it in order of what getimagesize() returns. getimagesize() returns the width, height, type, width and height. so to get just the type you would go like this:
Code: [Select]
if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
 
$userfile=$_FILES['userfile']['tmp_name'];
$userfile_size=$_FILES['userfile']['size'];
$userfile_type=$_FILES['userfile']['type'];
$userfile_name=$_FILES['userfile']['name'];
 
list($width, $height, $type) = getimagesize($userfile);

if($type != 2){
    die("Error not a jpeg");
Good luck with your coding.
Jason ~ ProjectFear ~ Jaysonic (and variations of... :))

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #11 on: November 11, 2006, 02:40:21 PM »
thanks that works perfectly!

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #12 on: November 11, 2006, 02:46:18 PM »
One last thing.......
I want this code to to report an error when someone enters a wrong password or username.

Code: [Select]
<?PHP
 $Uname='';
 $Pass='';
 $contents='';

 if ($_REQUEST['Uname'] != "") {
  $Uname = $_REQUEST['Uname'];
 } else {
die('You need both a Username and password to login');
 }



 if ($_REQUEST['Pass'] != "") {
  $Pass = $_REQUEST['Pass'];
 } else {
  die('You need both a Username and password to login');
 }



 $fp = fopen("imgs/".$Uname.".nam","rb");
 $contents = fread($fp, filesize("imgs/".$Uname.".nam"));
 fclose($fp);

 $pieces = explode(" ", $contents);

 if ($pieces[2] == md5($Pass)) {
setcookie('User',$Uname,time()+(60*60));
 } else {
die ('Sorry the password username combanation does not exist');
 }


header("Location: http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/index.php");


?>


the current checker is.....
Code: [Select]
if ($pieces[2] == md5($Pass)) {
setcookie('User',$Uname,time()+(60*60));
 } else {
die ('Sorry the password username combanation does not exist');
 }

but this wont work if you cant even open the file .usr so i need something to report when the file isnt there.

I feel the code should be placed right after this code
Code: [Select]
$fp = fopen("imgs/".$Uname.".nam","rb");
« Last Edit: November 11, 2006, 02:50:23 PM by allaboutthekick »

Offline Jaysonic

  • Fanatic
  • Posts: 3,332
  • Gender: Male
  • 8548863
    • View Profile
    • I Am Lewy Babes
Re: cant check file extension correctly
« Reply #13 on: November 11, 2006, 07:06:58 PM »
just add this wherever:

[php]
if(!file_exists("imgs/".$Uname.".nam")){
//code to execute if the file doesn't exist
}else{
//code to execute if the file exists
}
Good luck with your coding.
Jason ~ ProjectFear ~ Jaysonic (and variations of... :))

Offline allaboutthekickTopic starter

  • Irregular
  • Posts: 22
    • View Profile
Re: cant check file extension correctly
« Reply #14 on: November 11, 2006, 09:00:58 PM »
ok i implemented it here---

Code: [Select]
<?PHP
 $Uname='';
 $Pass='';
 $contents='';
 
if (!file_exists("imgs/".$Uname.".nam")){
echo "Username does not exsist, please check that capslock is not on and that your entry was spelled correctly.";
}else{
 if ($_REQUEST['Uname'] != "") {
  $Uname = $_REQUEST['Uname'];
 } else {
die('You need both a Username and password to login');
 }



 if ($_REQUEST['Pass'] != "") {
  $Pass = $_REQUEST['Pass'];
 } else {
  die('You need both a Username and password to login');
 }



 $fp = fopen("imgs/".$Uname.".nam","rb");
 $contents = fread($fp, filesize("imgs/".$Uname.".nam"));
 fclose($fp);

 $pieces = explode(" ", $contents);

 if ($pieces[2] == md5($Pass)) {
setcookie('User',$Uname,time()+(60*60));
 } else {
die ('Sorry the password username combanation does not exist');
 }


header("Location: http://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/index.php");

}
?>

but no matter what, right entry or wrong entry it states my echo.