Location: PHPKode > projects > PhpProjectMaster > www/inc/upload_utils.php
<?php
/* 
 Upload Handling functions library for PHP
 (c) 2004-2007 by "Oleg Savchuk" <hide@address.com>

 part of phpProjectMaster project
 http://phpprojmaster.sourceforge.net

 The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
 http://www.gnu.org/copyleft/gpl.html
*/

 require_once('image_utils.php');

//extensions upload functions check for (ondisk)
$UPLOAD_EXT=array('jpg','gif','png','swf');
$IMAGES_EXT=array(
 'jpg' => 1,
 'gif' => 1,
 'png' => 1,
 'bmp' => 1,
);

$ALLOWED_EXT_UPLOAD1=array('doc'=>1,'rtf'=>1,'txt'=>1,'eml'=>1,'html'=>1);
$ALLOWED_EXT_UPLOAD2=array('gif'=>1,'jpg'=>1);


// **************************************
// return: 
// -2 - empty item_id or $upload_path or $field_name
// -1 - upload failed
// 0 - no file
// 1 - upload successfull
function upload_file($item_id, $field_name, $exts, $upload_path, $opt=array()){
 global $err_msg;
 $result=0;

 if (!item_id || !$upload_path || !$field_name) return -2;

 $hexts=array_flip($exts);
 if ( check_uploaded_file($field_name, $hexts) ){
    cleanup_upload($item_id, $upload_path);

    $path_parts=pathinfo($_FILES[$field_name]['name']); #get the extension

    $file_path=get_upload_path($item_id, $upload_path, '', strtolower($path_parts['extension']), 'nocheck');
    $file_path_preview=get_upload_path($item_id, $upload_path, '_s', strtolower($path_parts['extension']), 'nocheck');

    mkdir_tree(dirname($file_path));

    if ( !proceed_uploaded_file($field_name, $file_path) ){
       $err_msg=lng("Warning! File wasn't uploaded. Try again.");
       $result=-1;
    }else{  //uploaded successfully
       $result=1;

       //check options
       //1. create preview?
       if ($opt['preview']){
          if (!$opt['preview_maxw']) $opt['preview_maxw']=$GLOBALS['MAX_PREVIEW_IMG_WIDTH'];
          if (!$opt['preview_maxh']) $opt['preview_maxh']=$GLOBALS['MAX_PREVIEW_IMG_HEIGHT'];
          image_resize($file_path, $opt['preview_maxw'], $opt['preview_maxh'], $file_path_preview);
       }

       //2. resize original?
       if ($opt['resize']){
          if (!$opt['resize_maxw']) $opt['resize_maxw']=$GLOBALS['MAX_FULL_IMG_WIDTH'];
          if (!$opt['resize_maxh']) $opt['resize_maxh']=$GLOBALS['MAX_FULL_IMG_HEIGHT'];
          image_resize($file_path, $opt['resize_maxw'], $opt['resize_maxh']);
       }
    }

 }else{
   $result=0;
 }

 return $result;
}

// **************************************
// check for extensions and size (TODO)
function check_uploaded_file($field_name, $allowed_exts){
 $ext='';
 if ($_FILES[$field_name] && $_FILES[$field_name]['name']>''){
    $orig_filename=$_FILES[$field_name]['name'];
    preg_match("/\.([^\.]+)$/", $orig_filename, $matches);
    $ext=strtolower($matches[1]);
    if ($ext=='jpeg') $ext='jpg';
   
    if ( count($allowed_exts) && !array_key_exists($ext, $allowed_exts) ){
       $ext='';
    }
    //also we could check for file size here if necessary $_FILES[$field_name]['name']
 }
 return $ext;
}

// **************************************
function proceed_uploaded_file($field_name, $save_filename){
 global $site_root;

// $orig_filename=$_FILES[$field_name]['name'];
// preg_match("/\.([^\.]+)$/", $orig_filename, $matches);
// $ext=$matches[1];

 $filename=$_FILES[$field_name]['tmp_name'];
 $res1=move_uploaded_file($filename, "$save_filename");

// rw($orig_filename);
// rw($ext);
// rw($filename);
// rw($res1);
 return $res1;
}

// ******************
// $nameadd - used for adding '_s' for previews
// $nocheck - don't check for file existence (used with $ext set to initially create file)
function get_upload_url($id, $basedir, $baseurl, $nameadd='', $ext='', $nocheck=0){
 global $site_root, $root_url;

 $path=id2dir($id)."/".$id.$nameadd;
 $diskpath=$basedir.$path;

 if (!$nocheck){
    if ($ext){
       if (!file_exists("$diskpath.$ext")){
          return;
       }
    }
    else{
       foreach ($GLOBALS['UPLOAD_EXT'] as $value){
          if (file_exists("$diskpath.$value")){
             $ext=$value;
          }
       }
       if (!$ext) return '';
    }
 }

 return $root_url.$baseurl.$path.'.'.$ext;
}

// ******************
function get_upload_path($id, $basedir, $nameadd='', $ext='', $nocheck=0){
 global $site_root, $root_url;

 $path=id2dir($id)."/".$id.$nameadd;
 $diskpath=$basedir.$path;

 if (!$nocheck){
    if ($ext){
       if (!file_exists("$diskpath.$ext")){
          return;
       }
    }
    else{
       foreach ($GLOBAL['UPLOAD_EXT'] as $value){
          if (file_exists("$diskpath.$value")){
             $ext=$value;
          }
       }
       if (!$ext) return '';
    }
 }

 return $basedir.$path.'.'.$ext;
}

########### remove all uploaded files related to $item_id
function cleanup_upload($id, $basedir){

 $path="/".id2dir($id)."/".$id;
 $diskpath=$basedir.$path;

 $preview_add='_s';

 foreach ($GLOBALS['UPLOAD_EXT'] as $value){
    @unlink("$diskpath.$value");
    @unlink("$diskpath$preview_add.$value");
 }
}


########### id to directory
# 1,000,000,001 => /1/0/0
function id2dir($id, $level=3){

 $id1=floor($id/1000);
 $result="";
 for($i=1;$i<=$level;$i++){
     if ($i==$level){
        $result=$id1.(strlen($result)?"/":"").$result;
        break;
     }
     $id2=floor($id1/1000);
     $result=($id1-$id2*1000).(strlen($result)?"/":"").$result;
     $id1=$id2;
 }

 return "/".$result;
}

// create directory tree
function mkdir_tree($dir){

 $parts=explode("/",$dir);

 $full_path='';
 for($i=0;$i<count($parts);$i++){
    $full_path.=$parts[$i]."/";
//    rw("create $parts[$i] => $full_path");
    if ( !file_exists($full_path) ) mkdir($full_path);
 }

}

//*********** return unchanged if not =='jpeg'
function jpeg2jpg($str){

 if ($str=='jpeg') $str='jpg';
 return $str;
}

?>
Return current item: PhpProjectMaster