upload file class not working 100%

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amzul
    New Member
    • Oct 2007
    • 130

    #1

    upload file class not working 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 :
    [CODE=php]<?php
    class image_upload
    {
    var $tmp_image;
    var $max_file_size = 5120;
    var $max_width = 200;
    var $max_height = 200;
    var $allow_types = array('image/jpeg','image/gif');
    var $errors;
    var $good;
    function is_image()
    {
    if(exif_imagety pe($this->tmp_image['tmp_name'])=="IMAGETYPE_G IF" && exif_imagetype( $this->tmp_image['tmp_name']) == "IMAGETYPE_JPEG ")
    {
    $this->good[] = 'check ex_if';
    $this->errors[] = 'Uploaded file is not image';
    return true;
    }
    else
    {
    $this->errors[] = 'Uploaded file is not image';
    return false;
    }
    }
    function is_type()
    {
    if (in_array($this->tmp_image['type'],$this->allow_types) )
    {
    $this->good[] ='check image [type]';
    return true;
    }
    else
    {
    $this->errors[] = 'Image type is not acceptable';
    return false;
    }
    }

    function is_size()
    {
    if ($this->tmp_image['size'] <= $this->max_file_siz e)
    {
    $this->good[2] ='check image [size]';
    return true;
    }
    else
    {
    $this->errors[] = 'Image file size is too big';
    return false;
    }
    }

    function is_dimensions()
    {
    $size = getimagesize($t his->tmp_image['tmp_name']);
    $width = $size[0];
    $height = $size[1];
    if ($width <= $this->max_width && $height <= $this->max_height)
    {
    $this->good[] ='check image [demenation]';
    return true;
    }
    else
    {
    $this->errors[] = 'Image is too height or too width';
    return false;
    }
    }

    function upload_image ($dest, $safe=false)
    {

    if ($safe)
    {
    $status = true;
    $status = $this->is_image();
    $status = $this->is_type();
    $status = $this->is_size();
    $status = $this->is_dimensions( );
    return ($status)?move_ uploaded_file($ this->tmp_image['tmp_name'], $dest):false;
    }
    else
    {
    return move_uploaded_f ile($this->tmp_image['tmp_name'], $dest);
    }
    }
    }
    ?>[/CODE]

    and the other part looks like that :

    [CODE=php]<?php
    include('upload .class.php');
    $IM = new image_upload();
    if ($_FILES['photo'])
    {
    $IM->tmp_image = $_FILES['photo'];
    if ($IM->upload_image($ _SERVER['DOCUMENT_ROOT'].'/tnimg/'.$IM->tmp_image['name'], true))
    {
    foreach ($IM-> good as $res)
    {
    $pic=$res."<br> ";
    }
    }
    else
    {
    foreach ($IM->errors as $error)
    {
    $pic = $error.'<br />';
    }
    }
    }
    ?>[/CODE]

    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
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    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_siz e = 5120;
    $this->max_width = 200;
    }
    //its bad example because in real world it should look like that:

    function image_upload($m ax_file_size,$m ax_width)
    {
    $this->max_file_siz e = $max_file_size;
    $this->max_width = $max_width;
    }
    //the rest of class code
    //end of class
    // and you should declare it like that:
    myFileUploadObj ect = new image_upload(51 20,200);
    [/php]
    i hope that helps

    regards
    jx2

    Comment

    • Amzul
      New Member
      • Oct 2007
      • 130

      #3
      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:
      [code=php]
      $status = $this->is_image();
      if($status)
      {
      $status = $this->is_type();
      }
      if ($status)
      {
      $status = $this->is_size();
      }
      if ($status)
      {
      $status = $this->is_dimensions( );
      }
      [/code]
      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_imagety pe($this->tmp_image['tmp_name'])==IMAGETYPE_GI F||exif_imagety pe($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
      Last edited by Atli; Nov 9 '07, 12:42 PM. Reason: Added [code] tags.

      Comment

      Working...