标题: 老外写的一个文件上传的zf虚拟类(转载)
c61811
新手上路
Rank: 1



UID 1182
精华 1
积分 10
帖子 27
翻译 0
原创 1
阅读权限 10
注册 2008-5-15
状态 离线
发表于 2008-7-9 11:35  资料  短消息  加为好友 
老外写的一个文件上传的zf虚拟类(转载)

source:http://framework.zend.com/wiki/display/ZFPROP/Zend_Http_Upload+-+Thomas+Weidner

class Zend_Http_Upload_Exception extends Zend_Exception {}  
class Zend_Http_Upload {  
    /**
       * Constructor
       * @param String|Array $files     - OPTIONAL single or array of files(fields)
       * @param Array          $options - OPTIONAL Array of options to set for all files
       */
    public function __construct($files = null, array $options = Array()) {}  
    /**
       * Add single file to upload from form
       * @param String $file      - single file(field)
       * @param Array    $options - OPTIONAL Array of options to set for the file
       */
    public function addFile($file, array $options = Array()) {}  
    /**
       * Add multiple files to upload from form
       * @param Array $files     - Array of files(fields)
       * @param Array $options - OPTIONAL Array of options to set for all files
       */
    public function addFiles($files, array $options = Array()) {}  
    /**
       * Set options, general for all files
       *      'destination' => destination to move files to
       *      'maxsize'       => maximum complete size to set
       *      'filetype'      => filetypes to set, multiple delimiter-seperated
       *      'exists'        => what to do if the file exists  
       *                      (REPLACE, ERROR, String* for autorename and/or other destination)
       * @param Array $options - Array of options to set for all files
       */
    public function setOptions(Array $options = Array()) {}  
    /**
       * Set maximum filesize
       * @param Integer        $size    - Maximum filesize for the files
       * @param String|Array $files - OPTIONAL Files for which to set the maximum size, if not set for all in sum
       */
    public function setMaxSize($size, $files = null) {}  
    /**
       * Set mime type
       * @param Integer        $type    - Mime type for the files
       * @param String|Array $files - OPTIONAL Files for which to set the type, if not set for all in sum
       */
    public function setFileType($type, $files = null) {}  
    /**
       * Set directory to store files, can be set per single file
       * @param String         $destination - Where to store the file/s
       * @param String|Array $files         - OPTIONAL Files for which to set the destination
       *                                      , if not set for all in sum
       */
    public function setDestination($destination, $files = null) {}  
    /**
       * Set the action for when the file exists already
       * @param String         $action - What to do when files exists
       *                                (REPLACE, ERROR, String* for autorename and/or other destination)
       * @param String|Array $files    - OPTIONAL Files for which to set the action
       */
    public function setExists($action, $files = null) {}  
    /**
       * Set a function which has to be used for checking the file
       * @param String         $function - Function which should be called
       */
    public function setCheck($function) {}  
    /**
       * Move files to specified directory, can be set per single file
       * @param String|Array $files     - OPTIONAL Files to move
       * @param Array          $options - OPTIONAL Options for the moving files to set temporary
       * @throws Zend_Http_Upload_Exception
       */
    public function move($files = null, array $options = array()) {}  
    /**
       * Checks all or the given files with all set restrictions.
       * This can be size, type, name, existence, or even an self defined check...
       *
       * @param String|Array $files     - OPTIONAL Files to check
       * @param Array          $options - OPTIONAL Options overriding actual checking parameters temporary
       * @throws Zend_Http_Upload_Exception
       */
    public function check($files = null, array $options = array()) {}  
    /**
       * Check if all or the given files were uploaded
       * @param String|Array $files - OPTIONAL Files to check if they were uploaded
       */
    public function isUploaded($files = null) {}  
    /**
       * Check if all or the given files have the given filetype
       * @param String|Array $files - OPTIONAL Files for which to check the type
       */
    public function isFileType($type, $files = null) {}  
    /**
       * Get actual upload progress, works only for php 5.2 with installed pecl extension!
       */
    public function getProgress() {}  
    /**
       * Returns all files to upload, should be used to generate the upload form
       * to prevent problems with wrong upload filenames
       */
    public function getFiles() {}  
}



Simplest validation-example:
$upload = new Zend_Http_Upload();  
// alternative syntax: addFile(new Zend_Http_Upload_File('docfile'));
$upload->addFile('docfile');  
// set max size for all files (in byte)
$upload->setMaxTotalSize(100000);  
// set max size for a single file ('docfile' can just be 50kB big, not 100kB)
$upload->setMaxSingleSize(50000);  
// syntax from Sylvains class
$upload->setValidExtensions(array('doc', 'odt', 'xml'), 'accept');  
// catch all errors from all files at once (constant randomly named)
if($upload->isValid() != UPLOAD_ERROR_NONE) {   
   // display error..
} else {  
   // process upload..
}  
// catch just errors from 'docfile' (in our case this is like the first example)
if($upload->docfile->isValid() != UPLOAD_ERROR_NONE) {  
   // display error..
} else {  
   // process upload..
}


A bit more complicated example:
$upload = new Zend_Http_Upload();  
// init new image-upload
$image = new Zend_Http_Upload_File_Image('photo');  
// image can just have a filesize of 100000 byte
$image->setMaxSize(100000);  
// set min and max image size (using image-functions)
$image->setMinDimension(100, 100);  
$image->setMaxDimension(500, 500);  
$upload->addFile($image);  
$photoValid = $upload->photo->isValid();  
if($photoValid == UPLOAD_ERROR_IMG_MINSIZE) {  
   // error no. 1
} else
if($photoValid == UPLOAD_ERROR_IMG_MAXSIZE) {  
   // error no. 2
} else
if($photoValid != UPLOAD_ERROR_NONE) {  
   // general error
} else {  
   // proccess upload..
}

顶部
 


PHPEye开源社区


当前时区 GMT+8, 现在时间是 2008-11-21 19:09

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.019462 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver