Look at the project requirements: You can choose whether to save 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 max upload capacity: Default 100Kb
- * @param string $model upload module
- * @param bool $insert write to Database
- * @param bool $thumb whether to generate thumbnails
- * @param string $wh The width of the thumbnail
- * 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 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 picture naming rules
- if (! $upload->upload ()) {
- return $upload->geterrormsg ();
- }else{
- $uploadlist = $upload->getuploadfileinfo ();
- }
- if ($insert) {
- return $this->_insert ($uploadlist);
- }else{
- return $uploadlist;
- }
- }
- /*
- * uploaded attachments are consolidated into attach data, deposited into the table and returned to 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 (ten) is not NULL auto_increment,
- ' Name ' varchar (+) not NULL COMMENT ' attachment name ',
- ' hashname ' varchar default NULL,
- ' Status ' tinyint (1) Default ' 1 ' COMMENT ' attachment status {1: enabled, 0: disabled} ',
- ' Savepath ' varchar (+) default NULL COMMENT ' storage address ',
- ' bsize ' varchar (+) Default NULL COMMENT ' attachment size ',
- ' model_name ' varchar default NULL COMMENT ' belongs to module ',
- ' user_id ' int (ten) default NULL COMMENT ' upload user ID ',
- ' Create_time ' int (ten) default NULL COMMENT ' upload time ',
- PRIMARY KEY (' id ')
- ) Engine=innodb DEFAULT Charset=utf8;
Copy Code |