PHPEye开源社区 » Zend Framework 使用讨论 » 老外写的一个文件上传的zf虚拟类(转载)
《Programming PHP》第二版上市
2008-7-9 11:35 c61811
老外写的一个文件上传的zf虚拟类(转载)

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


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

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

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.