Check the project requirements: You can choose whether to save the file to the database, specify the file type, file size, thumbnail and thumbnail size.
- Class AttachModel extends Model {
- /**
- * Attachment Upload
- * @ Param string $ type: jpg, png
- * @ Param int $ maxsize maximum upload capacity: 100Kb by default
- * @ Param string $ module in which the model is uploaded
- * @ Param bool $ whether the insert statement is written to the database
- * @ Param bool $ whether thumb generates a thumbnail
- * @ Param string $ wh width and height of the thumbnail
- * Example: $ upload-> upload (null, 102400, APP_NAME, true, true, array ('20140901', '20160901 '));
- */
- Public function upload ($ type = null, $ maxsize = '000000', $ model = null, $ insert = true, $ thumb = false, $ wh = array ('000000 ', '20140901 ')){
- // 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 = '/cms/tpl/Index/Public/Uploads/'. $ model .'/';
- } Else {
- $ Upload-> savePath = '/cms/tpl/Index/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 by attach, stored in the table, and an array is returned.
- **/
- 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;
- }
- }
- 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 ',
- '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;
|