473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

upload file class not working 100%

130 100+
hello all

i search the net for upload picture secure code
i fuond this code, and affter i changed it abit it looks like that :
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class image_upload
  3. {
  4.     var $tmp_image;
  5.     var $max_file_size = 5120;
  6.     var $max_width = 200;
  7.     var $max_height = 200;
  8.     var $allow_types = array('image/jpeg','image/gif');
  9.     var $errors;
  10.     var $good;
  11. function is_image()
  12. {
  13.     if(exif_imagetype($this->tmp_image['tmp_name'])=="IMAGETYPE_GIF" && exif_imagetype($this->tmp_image['tmp_name']) == "IMAGETYPE_JPEG") 
  14.     {
  15.         $this->good[] = 'check ex_if';
  16.         $this->errors[] = 'Uploaded file is not image';
  17.         return true;
  18.     }
  19.     else
  20.     {
  21.         $this->errors[] = 'Uploaded file is not image';
  22.         return false;
  23.     }
  24. }
  25. function is_type()
  26. {
  27.     if (in_array($this->tmp_image['type'],$this->allow_types))
  28.     {
  29.         $this->good[] ='check image [type]';
  30.         return true;
  31.     }
  32.     else
  33.     {
  34.         $this->errors[] = 'Image type is not acceptable';
  35.         return false;
  36.     }
  37. }
  38.  
  39. function is_size()
  40. {
  41.     if ($this->tmp_image['size'] <= $this->max_file_size)
  42.     {
  43.         $this->good[2] ='check image [size]';
  44.         return true;
  45.     }
  46.     else
  47.     {
  48.         $this->errors[] = 'Image file size is too big';
  49.         return false;
  50.     }
  51. }
  52.  
  53. function is_dimensions()
  54. {
  55.     $size = getimagesize($this->tmp_image['tmp_name']);
  56.     $width = $size[0];
  57.     $height = $size[1];
  58.     if ($width <= $this->max_width && $height <= $this->max_height)
  59.     {
  60.         $this->good[] ='check image [demenation]';
  61.         return true;
  62.     }
  63.     else
  64.     {
  65.         $this->errors[] = 'Image is too height or too width';
  66.         return false;
  67.     }
  68. }
  69.  
  70. function upload_image ($dest, $safe=false)
  71. {
  72.  
  73.     if ($safe)
  74.     {    
  75.         $status = true;
  76.          $status = $this->is_image();
  77.         $status = $this->is_type();
  78.         $status = $this->is_size();
  79.         $status = $this->is_dimensions();
  80.         return ($status)?move_uploaded_file($this->tmp_image['tmp_name'], $dest):false;
  81.     }
  82.     else
  83.     {
  84.         return move_uploaded_file($this->tmp_image['tmp_name'], $dest);
  85.     }
  86. }
  87. }
  88. ?>
and the other part looks like that :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include('upload.class.php');
  3. $IM = new image_upload();
  4. if ($_FILES['photo'])
  5. {
  6.     $IM->tmp_image = $_FILES['photo'];
  7.     if ($IM->upload_image($_SERVER['DOCUMENT_ROOT'].'/tnimg/'.$IM->tmp_image['name'], true))
  8.     {
  9.         foreach ($IM-> good as $res)
  10.         {
  11.             $pic=$res."<br>";
  12.         }
  13.     }
  14.     else
  15.     {
  16.         foreach ($IM->errors as $error)
  17.         {
  18.             $pic = $error.'<br />';
  19.         }
  20.     }
  21. }
  22. ?>
its working but not 100% first
its not going into the first 2 methods (i try to check useing the $good array) but nothing is showing. (exccept the dimantion part)

i went throuh the syntax over and over agian
maybe i better mind and new eyes can tell me why it dosnt check what it shouled check

BTW its uploading the files but not checking them thats the thing.
here is the site that i leeched it from
thanks
Nov 7 '07 #1
2 1532
jx2
228 100+
if i were you i woud write it on my own(it would lwt you understand whats going on)
i can promice i help but i try :-)

[php]
<?php
class image_upload
{
var $tmp_image;
//have you change this part?
var $max_file_size = 5120;
var $max_width = 200;
var $max_height = 200;
var $allow_types = array('image/jpeg','image/gif');
//--------- i am asking because i dont think it does work like that
// usualy if you want to initialize values of the class you should use
// cllass constructor i mean image_upload() in this case i guess thats why
// it doesnt seem to check what its loading
//i.e.:
function image_upload(){
$this->max_file_size = 5120;
$this->max_width = 200;
}
//its bad example because in real world it should look like that:

function image_upload($max_file_size,$max_width)
{
$this->max_file_size = $max_file_size;
$this->max_width = $max_width;
}
//the rest of class code
//end of class
// and you should declare it like that:
myFileUploadObject = new image_upload(5120,200);
[/php]
i hope that helps

regards
jx2
Nov 8 '07 #2
Amzul
130 100+
yes, i did write it agian.
the class affter the modification working fine
the part with the array is working, but u need to add image/pjpeg
to get all jpg files
and
function upload_image()
needs more modification
need to del the $status=true
and continiu only if the rest of the function return true like so:
Expand|Select|Wrap|Line Numbers
  1. $status = $this->is_image();
  2.         if($status)
  3.         {
  4.         $status = $this->is_type();
  5.         }
  6.         if ($status)
  7.         {
  8.         $status = $this->is_size();
  9.         }
  10.         if ($status)
  11.         {
  12.         $status = $this->is_dimensions();
  13.                                 }
  14.  
otherwize if the dimensions() return true u will recive it fine
oh and one last thing the
exif_imagetype need to be change like so:
if(exif_imagetype($this->tmp_image['tmp_name'])==IMAGETYPE_GIF||exif_imagetype($this->tmp_image['tmp_name'])== IMAGETYPE_JPEG)

i did add more stuff but.

the other did good job, i only modified it for my own use
sorry about tthe bad english but its tooo late here
Nov 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save...
3
by: Brian Birtle | last post by:
**** A CHALLENGE TO THE GURUS - refute the statement "It's impossible to build a file upload progress meter using ASP.NET" **** First person to prove me wrong gets "All Time .NET Programming GOD"...
10
by: Brian Henry | last post by:
Hi, I am having a problem with an attachment system I made... it works with files up to ~3MB in size then after that if you try to upload a file it just goes to a "Page can not be displayed" page...
5
by: bob garbados | last post by:
I am trying to create a database-driven photo gallery for a friend with an admin form to upload images... I can upload a file to the web server, but I want to store the image in a database and I...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
0
by: Kelvin.YuShen | last post by:
Hello there, I got a problem when I was uploading the skin I developed. Following is the code from my original skin template (skin.htm): <TABLE cellspacing="3" cellpadding="3" width="100%"...
1
by: gryffin | last post by:
im trying to do file extension checking but its not working :( i have the following in the head <script language="JavaScript"> extArray = new Array(".jpg", ".png", ".bmp"); function...
2
by: Event Horizon | last post by:
Hi, I'm trying to add an simple upload applet to shopping cart script. My new applet form sends all needed post fields ( quantity, product, etc... ) but the "file" post field is hardcoded in...
8
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.