PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old July 3rd, 2002, 06:56 AM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
File Upload Problem

Hey folks, i'm having some problems with this upload script of mine. I get a fair few errors, just wondering if anyone can spot the flaws...

Heres my script:

php Code:
Original - php Code
  1.  
  2. <?php
  3. // the following values are used to verify_uploaded_file()
  4. // as the types and sizes that are allowed to be uploaded.
  5. $UPLOAD_TYPES['JPG'] == 1; //Allow .jpg
  6. $UPLOAD_TYPES['JPEG'] == 1;
  7. $UPLOAD_SIZES['max'] == 100000;
  8. $UPLOAD_SIZES['min'] == 0;
  9.  
  10. echo 'File: ' . $HTTP_POST_FILES['upload_file']['name'] .
  11.      '<br>'
  12.      'Size: ' . $HTTP_POST_FILES['upload_file']['size'] .
  13.      '<br><br>';
  14.     
  15. //verify the files size and type
  16. $intResult = verify_uploaded_file(
  17.                     $HTTP_POST_FILES['upload_file']['name'],
  18.                     $HTTP_POST_FILES['upload_file']['size']);
  19.                    
  20. if($intResult == 1)
  21. {
  22.     echo $HTTP_POST_FILES['uploaded_file']['name'] .
  23.             ' is acceptable. ';
  24. }
  25. else
  26. {
  27.     echo $HTTP_POST_FILES['uploaded_file']['name'] .
  28.             ' is unacceptable. ';
  29.     if($intResult == -1)
  30.     {
  31.         echo 'Reason: File Size out of Allowed Range.';
  32.     }
  33.     elseif($intResult == -2)
  34.     {
  35.         echo 'Reason: File type not allowed';
  36.     }
  37. }
  38.    
  39. function verify_uploaded_file($strName, $intSize)
  40. /*
  41. PRE: $strName and $intSize are attributes taken from
  42. the uploaded files information.
  43. Also, the global variables $UPLOAD_SIZES and
  44. $UPLOAD_TYPES should be defined prior to calling this function
  45. PST: Returns
  46.     1 if the file is acceptable
  47.     -1 if the files size is out of range
  48.     -2 if the files type is unacceptable
  49. */
  50. {
  51.     //check the file size
  52.     if($intSize < $GLOBALS['UPLOAD_SIZES']['min'] ||
  53.         $intSize > $GLOBALS['UPLOAD_SIZES']['max'])
  54.     {
  55.         return -1;
  56.     }
  57.    
  58.     //check the file type
  59.     $arrSegments = split('[.]', $strName); //may contain multiple dots
  60.     $strExtension = $arrSegments[count($arrSegments) - 1];
  61.    
  62.     if($GLOBALS['UPLOAD_TYPES'][strtoupper($strExtension)] != 1)
  63.     {
  64.         return -2; //file type not defined / allowed
  65.     }
  66.    
  67.     //all tests have passed, the file is valid
  68.     return 1;
  69. }
  70.  
  71.  
  72. //define mysql login settings
  73. $host = 'localhost';
  74. $user = 'username';
  75. $pass = '******';
  76. $db = 'database';
  77.  
  78. //this is the file that will be inserted into the database
  79. $MY_FILE = $HTTP_POST_FILES['upload_file'];
  80.  
  81. //open the file and store its contents in $file_contents
  82. $file = fopen($MY_FILE, 'r');
  83. $file_contents = fread($file);
  84. fclose($file);
  85.  
  86. // we need to escape strange chars that might appear in $file_contents
  87. $file_contents = addslashes($file_contents);
  88.  
  89. //put the file in the database
  90. mysql_connect($host, $user, $pass)
  91.     or die("Couldnt connect to database");
  92.     or die("couldnt select database");
  93. mysql_query("INSERT INTO members SET avatar = '$file_contents'")
  94.     or die( mysql_error() );
  95.  
  96.  
  97. ?>


cheers in advance...

Reply With Quote
  #2  
Old July 3rd, 2002, 08:27 AM
CmdrDats CmdrDats is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: <br><img src='http://www.dats.co.za/icon.gif'>
Posts: 269 CmdrDats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to CmdrDats Send a message via AIM to CmdrDats Send a message via Yahoo to CmdrDats
RE: File Upload Problem

Give us a couple of your errors? just about the top 10 should be enough..

Reply With Quote
  #3  
Old July 3rd, 2002, 01:35 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: File Upload Problem

I'm with CmdrDats on this one..let's see some of those errors...but, for starters:

$UPLOAD_TYPES['JPG'] == 1; //Allow .jpg
$UPLOAD_TYPES['JPEG'] == 1;
$UPLOAD_SIZES['max'] == 100000;
$UPLOAD_SIZES['min'] == 0;

on all those you are doing comparisions. Don't you want to do assignments? I.e. one equal sign...


Reply With Quote
  #4  
Old July 3rd, 2002, 02:07 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: File Upload Problem

yes matt, i certainly did want to do assignments

here are some errors i'm getting:

Parse error: parse error, expecting `','' or `';'' in /home/sites/site93/web/testing_arena/upload2.php on line 19
which is this line:
php Code:
Original - php Code
  1. 'Size: ' . $HTTP_POST_FILES['upload_file']['size'] .


then theres these
php Code:
Original - php Code
  1.  
  2. Warning: fopen("Array", "r") - No such file or directory in /home/sites/site93/web/testing_arena/upload2.php on line 21
  3.  
  4. Warning: Wrong parameter count for fread() in /home/sites/site93/web/testing_arena/upload2.php on line 22
  5.  
  6. Warning: Supplied argument is not a valid File-Handle resource in /home/sites/site93/web/testing_arena/upload2.php on line 23
  7.  
  8. //which is:
  9. $file = fopen($MY_FILE, 'r');
  10. $file_contents = fread($file);
  11. fclose($file);


if i fix these ones i should be able to sort it out hopefully..

Reply With Quote
  #5  
Old July 3rd, 2002, 02:16 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: File Upload Problem

This:

echo 'File: ' . $HTTP_POST_FILES['upload_file']['name'] .
'<br>'
'Size: ' . $HTTP_POST_FILES['upload_file']['size'] .
'<br><br>';

should be: (note the added period):

echo 'File: ' . $HTTP_POST_FILES['upload_file']['name'] .
'<br>' .
'Size: ' . $HTTP_POST_FILES['upload_file']['size'] .
'<br><br>';

try changing:

$MY_FILE = $HTTP_POST_FILES['upload_file'];

to:

$MY_FILE = $HTTP_POST_FILES['upload_file']['tmp_name'];



Reply With Quote
  #6  
Old July 3rd, 2002, 03:15 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: File Upload Problem

Thanks, that fixed most problems, although now it keeps saying it is too large when it is under the limit

Reply With Quote
  #7  
Old July 3rd, 2002, 03:26 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: File Upload Problem

I'd be interested to know what size file you are trying with..remember 100000 bytes equates to 97.65 kilobytes....


Reply With Quote
  #8  
Old July 3rd, 2002, 03:31 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: File Upload Problem

the file i'm trying with is a 13.6kb .jpg file. I also tried to upload a .txt file and i got the error: file too large

Reply With Quote
  #9  
Old July 3rd, 2002, 03:42 PM
D1NGO D1NGO is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Perth, Australia
Posts: 221 D1NGO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: File Upload Problem

hmmm...ok, i just realised that it does actually insert it into the database, yet i still get the error message

Reply With Quote