php常用文件上传类
php常用文件上传类
<?php /** * flie class * (jpg,gif,png) */ class Upload { var $_file; var $_fileType; var $target = 'upload/'; /** * construct for this class * * @param string $name * @return Upload */ function Upload($name) { if (isset($_FILES[$name])) { $this->_file = & $_FILES[$name]; $this->_parseUploadFile(); } else { die('No file upload.'); } } /** * set upload target path * * @param string $path * @return boolean */ function setTarget($path = 'upload/') { if (is_dir($path)) { $this->target = rtrim($path, '/') . '/'; return true; } else { return false; } } /** * get the type of the file * */ function _parseUploadFile() { $type = $this->_file['type']; if (isset($type) && $type != '') { switch ($type) { case 'image/gif': $this->_fileType = 'gif'; break; case 'image/png': $this->_fileType = 'png'; break; case 'image/jpeg': $this->_fileType = 'jpg'; break; case 'image/pjpeg': $this->_fileType = 'jpg'; break; default: $this->_fileType = 'unknow'; break; } } else { $filename = $this->_file['name']; $filename = explode('.', $filename); $filename = strtoupper($filename[sizeof($filename) - 1]); switch ($filename) { case 'PNG': $this->_fileType = 'png'; break; case 'JPEG': $this->_fileType = 'jpg'; break; case 'JPG': $this->_fileType = 'jpg'; break; case 'GIF': $this->_fileType = 'gif'; break; default: $this->_fileType = 'unknow'; break; } unset($filename); } unset($type); } /** * upload file * * @return array */ function load() { if ($this->_fileType == 'unknow') { die('Can not upload this file,because the type is not allow.'); } if (file_exists($this->_file['tmp_name']) && is_uploaded_file($this->_file['tmp_name'])) { $new_file_name = $this->target . time() . '.' . $this->_fileType; move_uploaded_file($this->_file['tmp_name'], $new_file_name); return array( 'name' => $new_file_name, 'size' => $this->_file['size'], 'type' => $this->_fileType ); } else { return false; } } }
本文地址:http://www.phprm.com/code/a59ea48267475fe60583c20abfe84c90.html
转载随意,但请附上文章地址:-)