FileUploader.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
00010 
00049 class FLEA_Helper_FileUploader
00050 {
00056     var $_files = array();
00057 
00063     var $_count;
00064 
00072     function FLEA_Helper_FileUploader($cascade = false)
00073     {
00074         if (is_array($_FILES)) {
00075             foreach ($_FILES as $field => $struct) {
00076                 if (!isset($struct['error'])) { continue; }
00077                 if (is_array($struct['error'])) {
00078                     $arr = array();
00079                     for ($i = 0; $i < count($struct['error']); $i++) {
00080 
00081                         if ($struct['error'][$i] != UPLOAD_ERR_NO_FILE) {
00082                             $arr[] =& new FLEA_Helper_FileUploader_File($struct, $field, $i);
00083                             if (!$cascade) {
00084                                 $this->_files["{$field}{$i}"] =& $arr[count($arr) - 1];
00085                             }
00086                         }
00087                     }
00088                     if ($cascade) {
00089                         $this->_files[$field] = $arr;
00090                     }
00091                 } else {
00092                     if ($struct['error'] != UPLOAD_ERR_NO_FILE) {
00093                         $this->_files[$field] =& new FLEA_Helper_FileUploader_File($struct, $field);
00094                     }
00095                 }
00096             }
00097         }
00098         $this->_count = count($this->_files);
00099     }
00100 
00106     function getCount()
00107     {
00108         return $this->_count;
00109     }
00110 
00116     function & getFiles()
00117     {
00118         return $this->_files;
00119     }
00120 
00128     function existsFile($name)
00129     {
00130         return isset($this->_files[$name]);
00131     }
00132 
00140     function & getFile($name)
00141     {
00142         if (!isset($this->_files[$name])) {
00143             FLEA::loadClass('FLEA_Exception_ExpectedFile');
00144             return __THROW(new FLEA_Exception_ExpectedFile('$_FILES[' . $name . ']'));
00145         }
00146         return $this->_files[$name];
00147     }
00148 
00156     function isFileExist($name)
00157     {
00158         return isset($this->_files[$name]);
00159     }
00160 
00166     function batchMove($destDir)
00167     {
00168         foreach ($this->_files as $file) {
00169             /* @var $file FLEA_Helper_FileUploader_File */
00170             $file->move($destDir . '/' . $file->getFilename());
00171         }
00172     }
00173 }
00174 
00182 class FLEA_Helper_FileUploader_File
00183 {
00189     var $_file = array();
00190 
00196     var $_name;
00197 
00207     function FLEA_Helper_FileUploader_File($struct, $name, $ix = false)
00208     {
00209         if ($ix !== false) {
00210             $s = array(
00211                 'name' => $struct['name'][$ix],
00212                 'type' => $struct['type'][$ix],
00213                 'tmp_name' => $struct['tmp_name'][$ix],
00214                 'error' => $struct['error'][$ix],
00215                 'size' => $struct['size'][$ix],
00216             );
00217             $this->_file = $s;
00218         } else {
00219             $this->_file = $struct;
00220         }
00221 
00222         $this->_file['is_moved'] = false;
00223         $this->_name = $name;
00224     }
00225 
00232     function setAttribute($name, $value)
00233     {
00234         $this->_file[$name] = $value;
00235     }
00236 
00244     function getAttribute($name)
00245     {
00246         return $this->_file[$name];
00247     }
00248 
00254     function getName()
00255     {
00256         return $this->_name;
00257     }
00258 
00264     function isSuccessed()
00265     {
00266         return $this->_file['error'] == UPLOAD_ERR_OK;
00267     }
00268 
00274     function getError()
00275     {
00276         return $this->_file['error'];
00277     }
00278 
00284     function isMoved()
00285     {
00286         return $this->_file['is_moved'];
00287     }
00288 
00294     function getFilename()
00295     {
00296         return $this->_file['name'];
00297     }
00298 
00304     function getExt()
00305     {
00306         if ($this->isMoved()) {
00307             return pathinfo($this->getNewPath(), PATHINFO_EXTENSION);
00308         } else {
00309             return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
00310         }
00311     }
00312 
00318     function getSize()
00319     {
00320         return $this->_file['size'];
00321     }
00322 
00328     function getMimeType()
00329     {
00330         return $this->_file['type'];
00331     }
00332 
00338     function getTmpName()
00339     {
00340         return $this->_file['tmp_name'];
00341     }
00342 
00348     function getNewPath()
00349     {
00350         return $this->_file['new_path'];
00351     }
00352 
00363     function check($allowExts = null, $maxSize = null)
00364     {
00365         if (!$this->isSuccessed()) { return false; }
00366 
00367         if ($allowExts) {
00368             if (strpos($allowExts, ',')) {
00369                 $exts = explode(',', $allowExts);
00370             } elseif (strpos($allowExts, '/')) {
00371                 $exts = explode('/', $allowExts);
00372             } elseif (strpos($allowExts, '|')) {
00373                 $exts = explode('|', $allowExts);
00374             } else {
00375                 $exts = array($allowExts);
00376             }
00377 
00378             $filename = $this->getFilename();
00379             $fileexts = explode('.', $filename);
00380             array_shift($fileexts);
00381             $count = count($fileexts);
00382             $passed = false;
00383             $exts = array_filter(array_map('trim', $exts), 'trim');
00384             foreach ($exts as $ext) {
00385                 if (substr($ext, 0, 1) == '.') {
00386                     $ext = substr($ext, 1);
00387                 }
00388                 $fileExt = implode('.', array_slice($fileexts, $count - count(explode('.', $ext))));
00389                 if (strtolower($fileExt) == strtolower($ext)) {
00390                     $passed = true;
00391                     break;
00392                 }
00393             }
00394             if (!$passed) {
00395                 return false;
00396             }
00397         }
00398 
00399         if ($maxSize && $this->getSize() > $maxSize) {
00400             return false;
00401         }
00402 
00403         return true;
00404     }
00405 
00411     function move($destPath)
00412     {
00413         $this->_file['is_moved'] = true;
00414         $this->_file['new_path'] = $destPath;
00415         return move_uploaded_file($this->_file['tmp_name'], $destPath);
00416     }
00417 
00421     function remove()
00422     {
00423         if ($this->isMoved()) {
00424             unlink($this->getNewPath());
00425         } else {
00426             unlink($this->getTmpName());
00427         }
00428     }
00429 
00433     function removeMovedFile()
00434     {
00435         if ($this->isMoved()) {
00436             unlink($this->getNewPath());
00437         }
00438     }
00439 }

Generated at Sat Feb 2 15:18:50 2008 for FleaPHP by  doxygen 1.5.3