PHP Classes

File: _class_upload_file.php

Recommend this page to a friend!
  Classes of Fabrizio Parrella   bib_uploadfile   _class_upload_file.php   Download  
File: _class_upload_file.php
Role: Class source
Content type: text/plain
Description: main Class
Class: bib_uploadfile
Process uploaded files of images and other types
Author: By
Last change:
Date: 18 years ago
Size: 10,172 bytes
 

Contents

Class file image Download
<?php
/*
this class will be used for bibivu system to upload files/pictures
to the right location for each module
2005-04-20 - v.1.0
*/

class bib_upload{
    var
$error = array();
    var
$files = array();
    var
$file_max_size = 1048576;
    var
$_cfg = array();

    function
reset_all(){
       
$this->error['number'] = 0;
       
$this->error['text'] = '';
       
$this->error['fatal'] = false;
       
$this->files = array();
       
$this->file_new_name = ''; // replace the name body
       
$this->file_overwrite = false;

       
$this->file_max_size = 1048576;

       
$this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'

       
$this->image_resize = false; // resize the image
       
$this->image_x = 150;
       
$this->image_y = 150;
       
$this->image_ratio = false;
       
$this->image_ratio_no_zoom_in = false;
       
$this->image_ratio_no_zoom_out = false;
    }
    function
bib_upload($cfg){
       
$this->reset_all();
       
$this->_cfg['id'] = $cfg['id'];
       
$this->_cfg['depth'] = $cfg['depth'];
       
$this->_cfg['folder_site'] = $cfg['folder_site'];
       
$this->_cfg['folder_module'] = $cfg['folder_module'];
    }
    function
add_file($file){
// print_r($this->error);
       
if (!$file) {
           
$this->set_error('1000','File upload error. Please try again.',true);
        } else {
            if(
is_array($file['error'])){
               
//the files passed is an array of posts, I need to parse it
               
$ret = false;
                foreach(
$file['error'] AS $key=>$value){
                   
//I need to replace the same structure as the main array
                   
$f1 = array();
                    foreach(
$file AS $kkey=>$vvalue){
                       
$f1[$kkey] = $file[$kkey][$key];
                    }
                    if(
$ret==false){
                       
$ret = $this->add_file($f1);
                    }
                }
                return
$ret;
            } elseif(
$file['error'] === 0){
                if (
$file['name'] == '') {
                   
$this->set_error('1001','File upload error. Please try again.',true);
                } elseif (
$file['size'] > $this->file_max_size ) {
                   
$this->set_error('1002','File too big.',true);
                }
            } else {
                switch(
$file['error']) {
                    case
0:
                       
// all is OK
                       
break;
                    case
1:
                       
$this->set_error($file['error']+2000,'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).',false);
                        break;
                    case
2:
                       
$this->set_error($file['error']+2000,'File upload error (the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form).',false);
                        break;
                    case
3:
                       
$this->set_error($file['error']+2000,'File upload error (the uploaded file was only partially uploaded).',false);
                        break;
                    case
4:
                       
$this->set_error($file['error']+2000,'File upload error (no file was uploaded).',false);
                        break;
                    default:
                       
$this->set_error(2000,'File upload error (unknown error code).',true);
                        break;
                }
                return
false;
            }
        }
       
//there has been a fatal error
       
if($this->error['fatal']){
            return
false;
        } else {
           
$this->files[] = $file;
        }
        return
true;
    }
    function
new_name($which = 0, $new_name = '', $overwrite = false, $auto_rename = true){
       
//this function will apply to the last file uploaded
       
$cc = count($this->files);
        if(
$cc == 0 || empty($this->files)){
           
//there are not files to rename
           
$this->set_error(3001,'There are not files uploaded yet.',false);
        } else {
           
$cc--;
            if(
$which >= 0 && $which<=$cc) $cc = $which;
           
//if new_name is '', I have to create a random name
           
if($new_name == ''){
               
$new_name = strtoupper(substr(md5(time()*77+57),0,10));
            }
           
$this->files[$cc]['new_name'] = $new_name;
           
$this->files[$cc]['overwrite'] = $overwrite;
           
$this->files[$cc]['auto_rename'] = $auto_rename;
        }
    }
   
    function
resize($which = 0, $new_w=0, $new_h=0, $ratio = false){
       
//resize the image keeping the
       
$cc = count($this->files);
        if(
$cc == 0 || empty($this->files)){
           
//there are not files to rename
           
$this->set_error(3001,'There are not files uploaded yet.',false);
        } else {
           
$cc--;
            if(
$which >= 0 && $which<=$cc) $cc = $which;
           
$this->files[$cc]['new_w'] = $new_w;
           
$this->files[$cc]['new_h'] = $new_h;
           
$this->files[$cc]['ratio'] = $ratio;
        }
    }
   
    function
is_image($which=0, $is_image=true){
       
//resize the image keeping the
       
$cc = count($this->files);
        if(
$cc == 0 || empty($this->files)){
           
//there are not files to rename
           
$this->set_error(3001,'There are not files uploaded yet.',false);
        } else {
           
$cc--;
            if(
$which >= 0 && $which<=$cc) $cc = $which;
           
$this->files[$cc]['is_image'] = $is_image;
        }
    }
   
    function
process($module_code){
        if(
$this->error['fatal']){
            return
false;
        }
        if(
$module_code == ''){
           
$this->set_error('3001','Please Pass the module code .',true);
            return
false;
        } else {
           
$destpath = $this->_cfg['depth'].$this->_cfg['folder_site'].$this->_cfg['folder_module'].$module_code;
           
//check if the folder exist, if not, try to create it with chmod=777
           
if(!is_dir($destpath)){
               
$oldumask = umask(0);
               
mkdir($destpath,0777);
               
umask($oldumask);
                if(!
is_dir($destpath)){
                   
$this->set_error('3002','Impossible to create the module folder.',true);
                    return
false;
                }
            } elseif(!
is_writable($destpath)) {
               
$this->set_error('3003','Module folder is not writable.',true);
                return
false;
            }
           
$destpath .= '/';
            foreach(
$this->files AS $key=>$file){
                if(!isset(
$file['new_name']) || $file['new_name']==''){
                   
$file['new_name'] = $file['name'];
                }
               
$destfile = $destpath.$file['new_name'];

                if(isset(
$file['is_image'])){
                    if(isset(
$file['new_w']) && isset($file['new_h'])){
                       
//check if I have to resize the file
                       
$image_hw = @getimagesize($file['tmp_name']);
                        if(isset(
$image_hw) && !empty($image_hw)){
                           
$picwidth = $image_hw[0];
                           
$picheigth = $image_hw[1];
                            if( (
$file['new_w'] < $picwidth) ||
                                (
$file['new_h'] < $picheigth) ){
                               
//only reduce
                               
if(_NETPBM_PATH!=''){
                                    require_once
$this->_cfg['depth'].'files_includes/Transform.php';
                                   
$im = new Image_Transform;
                                   
$im->load($file['tmp_name']);
                                    if(
$file['ratio']){
                                        if(
$file['new_w'] < $picwidth){
                                           
$picheigth = (int)(($file['new_w']*100/$picwidth)*$picheigth/100);
                                           
$picwidth = $file['new_w'];
                                        }
                                        if(
$file['new_h'] < $picheigth){
                                           
$picwidth = (int)(($file['new_h']*100/$picheigth)*$picwidth/100);
                                           
$picheigth = $file['new_h'];
                                        }
                                    } else {
                                       
$picwidth = $file['new_w'];
                                       
$picheigth = $file['new_h'];
                                    }
                                   
$im->resize($picwidth, $picheigth);
                                   
$destfile = $destfile.'.'.$im->getWebSafeFormat();
                                   
$destfile = $this->_check_filename($destfile,$file['overwrite'],$file['auto_rename']);
                                    if(!
$destfile){
                                       
$this->set_error(4003,'Error renaming the file.',false);
                                    } else {
                                       
$im->save($destfile, $im->getWebSafeFormat());
                                       
$this->files[$key]['new_name'] = basename($destfile);
                                       
$im->free();
                                    }
                                } else {
                                   
//is not an image or the file is not a compatible pic
                                   
$this->set_error(4002,'NETPBM path not set.',false);
                                }
                            } else {
                               
//not resize is necessary
                               
$destfile = $this->_check_filename($destfile,$file['overwrite'],$file['auto_rename']);
                                if(!
$destfile){
                                   
$this->set_error(4003,'Error renaming the file.',false);
                                } else {
                                   
move_uploaded_file($file['tmp_name'],$destfile);
                                }
                            }
                        } else {
                           
//is not an image or the file is not a compatible pic
                           
$this->set_error(4001,'The file '.$file['name'].' is not resizable.',false);
                        }
                    } else {
                       
//the image is not resized
                       
if(_NETPBM_PATH!=''){
                            require_once
$this->_cfg['depth'].'files_includes/Transform.php';
                           
$im = new Image_Transform;
                           
$im->load($file['tmp_name']);
                           
$destfile = $destfile.'.'.$im->getWebSafeFormat();
                           
$destfile = $this->_check_filename($destfile,$file['overwrite'],$file['auto_rename']);
                            if(!
$destfile){
                               
$this->set_error(4003,'Error renaming the file.',false);
                            } else {
                               
$im->save($destfile, $im->getWebSafeFormat());
                               
$this->files[$key]['new_name'] = basename($destfile);
                               
$im->free();
                            }
                           
$im->save($destfile, $im->getWebSafeFormat());
                           
$this->files[$key]['new_name'] = basename($destfile);
                           
$im->free();
                        } else {
                           
$destfile = $this->_check_filename($destfile,$file['overwrite'],$file['auto_rename']);
                            if(!
$destfile){
                               
$this->set_error(4003,'Error renaming the file.',false);
                            } else {
                               
move_uploaded_file($file['tmp_name'],$destfile);
                            }
                        }
                    }
                } else {
                   
//not a pic
                   
$destfile = $this->_check_filename($destfile,$file['overwrite'],$file['auto_rename']);
                    if(!
$destfile){
                       
$this->set_error(4003,'Error renaming the file.',false);
                    } else {
                       
move_uploaded_file($file['tmp_name'],$destfile);
                    }
                }
                if(
file_exists($file['tmp_name'])){
                   
//cleaning
                   
@unlink($file['tmp_name']);
                }
            }
        }
       
//there has been a fatal error
       
if($this->error['fatal']){
            return
false;
        } else {
            return
$this->files;
        }
    }
    function
_check_filename($name, $overwrite, $auto_rename){
        if(
$overwrite == false){
            if(
file_exists($name)){
                if(
$auto_rename == true){
                   
ereg('\.([^\.]*$)', $name, $ext);
                   
$body = substr($name, 0, (strlen($name) - strlen($ext))-1);
                   
$i = 1;
                    while (
file_exists($name)) {
                       
$this->name = $body.'_'.$i.'.'.$ext;
                       
$i++;
                    }
                } else {
                    return
false;
                }
            } else {
               
//file do not exist
           
}
        } else {
           
//overwrite the file
       
}
        return
$name;
    }

    function
set_error($number, $text, $fatal = false){
       
$this->error['number'] = $number;
       
$this->error['text'] = $text;
       
$this->error['fatal'] = $fatal;
    }
}
?>