Take a look at the project requirements: you can choose whether to save to the database, specify the file type, file size, thumbnail and thumbnail size
- class AttachModel extends Model{
-
- /**
- * Attachment upload
- * @param string $type File type: jpg, png
- * @param int $maxsize Maximum upload capacity: Default 100Kb
- * @param string $model Upload module
- * @param bool $insert Whether to write Database
- * @param bool $thumb Whether to generate thumbnails
- * @param string $wh The width and height of the thumbnails
- * Example: $upload->upload(null,102400,APP_NAME,true,true,array('300' ,'225'));
- */
- public function upload($type = null, $maxsize = '102400', $model = null, $insert = true , $thumb = false, $wh = array('160', '120')){
- //Import the upload class
- import('ORG.NET.UploadFile');
- $upload = new UploadFile();
- $ upload->maxSize = $maxsize;
- if ($type){
- $type = explode(',', $type);
- $upload->allowExts = $type;
- }else{
- $upload-> ;allowExts = array('jpg','png','gif','jpeg');
- }
- if ($model){
- $upload->savePath = '../Public/Uploads/'.$ model.'/';
- }else{
- $upload->savePath = '../Public/Uploads/';
- }
- if ($thumb){
- $upload->thumb = true;
- $upload ->thumbPrefix = 'zj_';
- $upload->thumbMaxWidth = $wh[0];
- $upload->thumbMaxHeight = $wh[1];
- }
- $upload->saveRule = uniqid;/ /Upload image naming rules
- if (!$upload->upload()) {
- return $upload->getErrorMsg();
- }else{
- $uploadlist = $upload->getUploadFileInfo();
- }
- if ($insert){
- return $this->_insert($uploadlist);
- }else{
- return $uploadlist;
- }
- }
- /*
- * The uploaded attachments are integrated into the data required for attach and stored in the table And return the array
- * */
- private function _insert($uploadlist){
- $j = count($uploadlist);
- $v = array();
- foreach ($uploadlist as $key => $value)
- {
- $v[$key]['name'] = $value['name'];
- $v[$key]['hashname'] = $value['savename'];
- $v[$key][ 'savepath'] = substr($value['savepath'], 2);
- $v[$key]['bsize'] = $value['size'];
- $v[$key]['user_id' ] = $_SESSION[C('USER_AUTH_KEY')];
- $v[$key]['create_time'] = time();
- $v[$key]['model_name'] = APP_NAME;
- $this-> ;add($v[$key]);
- if($this->thumb)
- {
- $v[$key]['prefix'] = $this->thumbPrefix;
- }
- $v[$ key]['id'] = M('Attach')->getLastInsID();
- }
- return $v;
- }
-
- }
Copy code
- DROP TABLE IF EXISTS `zj_attach`;
- CREATE TABLE `zj_attach` (
- `id` int(10) NOT NULL auto_increment,
- `name` varchar(100) NOT NULL COMMENT 'Attachment name',
- `hashname ` varchar(100) default NULL,
- `status` tinyint(1) default '1' COMMENT 'Attachment status {1: enabled, 0: disabled}',
- `savepath` varchar(100) default NULL COMMENT 'Storage address' ,
- `bsize` varchar(100) default NULL COMMENT 'Attachment size',
- `model_name` varchar(50) default NULL COMMENT 'Module to which it belongs',
- `user_id` int(10) default NULL COMMENT 'Upload user id',
- `create_time` int(10) default NULL COMMENT 'Upload time',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy code
|