Location: PHPKode > scripts > Hippo Picture Base64 Encode > hippo-picture-base64-encode/hippo_base64_pic.php
<?php

/* hippo_base64_picfile class by DavidLanz
 
*/

class hippo_base64_pic
{
  var $upload_form;
  var $upload_dir;
  var $upfile_name;
  var $upfile_size;
  var $upfile_type;
  var $upfile_ext;  
  var $max_file_size;
  var $extensions;
  var $err_str;
  var $temp_dir;
  var $header_type;
  var $chunk_per_char=10;
  var $write_filename='data/hippo_base64_image.php';
  var $f_open;
  var $f_name;
  
  /* Constructor */
  function hippo_base64_pic()
  {
    $this->upload_dir = "upload";
    $this->extensions = array('gif', 'jpg');
    $this->copy_mode = 0;
    $this->max_file_size = 0;
  }
  
  function get_ext()
  {
    return strtolower(substr(strrchr($this->upfile_name, '.'), 1));
  }
  
  function check_form()
  {
    if(is_uploaded_file($_FILES[$this->upload_form]['tmp_name']))
    {
      $this->upfile_name = $_FILES[$this->upload_form]['name'];
      $this->upfile_size = $_FILES[$this->upload_form]['size'];
      $this->upfile_type = $_FILES[$this->upload_form]['type'];
      $this->upfile_ext = $this->get_ext();
      
      return true;
    }
    else
    {
      $this->err_str = "Upload Form Error: ".$this->upload_form."\n";
      switch($HTTP_POST_FILES['fupload']['error'])
      {
        case 0:
          $this->err_str .= "There was a problem with your upload.";
          break;
        case 1:
          $this->err_str .= "The file you are trying to upload is too big.";
          break;
        case 2:
          $this->err_str .= "The file you are trying to upload is too big.";
          break;
        case 3:
          $this->err_str .= "The file you are trying upload was only partially uploaded.";
          break;
        case 4:
          $this->err_str .= "You must select an file for upload.";
          break;
        default:
          $this->err_str .= "There was a problem with your upload.";
          break;
      }
      return false;
    }
  }

  function check_upload_size()
  {
    if($this->max_file_size == true)
    {
      if($this->upfile_size > $this->max_file_size) 
      {
        $this->err_str="Upload File Size Limit: ".$this->max_file_size." Bytes";
        return false;
      } 
    }
    return true;
  }

  function fix_dir($dir)
  {
    $dir = $this->upload_dir;

    if(is_dir($dir))
    {
      return true;
    }
    else
    {
      /*
      $this->err_str = "/".$dir." NOT exist.<BR>";
      return false;
      */
      mkdir($dir);
      return true;
    }

  }

  function if_uploadfile_exist()
  {
    if(file_exists($this->upload_dir . $this->upfile_name))
    {
      /*
      $this->err_str = "File: ". $this->upfile_name ." already exist.";
      return false;
      */
      /* overwrite into static file */
      return true;
    }
    return true;
  }
  
  function check_extensions()
  {
    if(!in_array($this->upfile_ext, $this->extensions))
    {
      $this->err_str = "Invalid File Extension.";
      return false;
    }
    else
    {
      return true;
    }
  }
  
  function delete_file($del_file, $dir)
  {
    if(!(is_dir($dir)))
    {
      $this->err_str = "/".$dir." not exist.<BR>";
      return false;
    }
    
    if(!@unlink($this->upload_dir.$del_file )) 
    {
      $this->error_msg("File: ".$del_file." can't be deleted, or not exist.");
      return false;
    } 
    
    return true;
  }
  
  function check_all()
  {
    if(!$this->check_form())
    {
      return false;
    }
    
    if(!$this->fix_dir($this->upload_dir))
    {
      return false;
    }
    
    if(!$this->if_uploadfile_exist())
    {
      return false;
    }
    
    if(!$this->check_upload_size())
    {
      return false;
    }
    
    $this->get_ext();
    if(!$this->check_extensions())
    {
      return false;
    }
    return true;
  }
  
  function hippo_writefile($fpn)
  {
    $this->f_name=$fpn;
    $this->create_newfile();
  }
  
  function create_newfile()
  {
    if($this->f_open = fopen($this->f_name, "w+"))
    {
      return true;
    }
    else
    {
      $this->err_str = "Cannot Create File.";
      return false;
    }
    fseek ($this->f_open, 0);
  }
  
  function write($wtf)
  {
    if(fwrite($this->f_open, $wtf."\n"))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  
  function close()
  {
    fclose($this->f_open);
  }
  
  function upload()
  {
    if(!$this->check_all())
    {
      return false;
    }
    //$this->temp_dir = $this->upload_dir . $this->upfile_name;
    $this->temp_dir = $this->upload_dir . 'image.gif';
    
    copy($_FILES[$this->upload_form]['tmp_name'], $this->temp_dir);
    
    $this->header_type = 'image/gif';
    
    $fo = fopen ($this->temp_dir, "r");
    $fr = fread ($fo, filesize($this->temp_dir));
    fclose ($fo);
    //$enc = chunk_split (base64_encode($fr));  // For Email MIME
    $enc = base64_encode($fr);
    
    $j=0;
    for($i=0;$i<strlen($enc);$i++)
    {
      if($i%$this->chunk_per_char==0 && $i!=0)
      {
        $j++;
        $res[$j] .= $enc[$i];
      }
      else
      {
        $res[$j] .= $enc[$i];
      }
    }
    
    $this->hippo_writefile($this->write_filename);
    $this->write('<?php');
    $this->write('header("Content-Type: '.$header_type.'");');
    $this->write('header("Content-Disposition: inline");');
    $this->write('echo base64_decode(');
    for($i=0;$i<count($res);$i++)
    {
      $this->write('"'.$res[$i].'".');
    }
    $this->write('"");?>');
    $this->close();
    
    return true;
  }
  
  function show_error()
  {
    return $this->err_str;
  }
  
}
?>
Return current item: Hippo Picture Base64 Encode