Whether ThinkPHP uploads images to the database class

Source: Internet
Author: User
Whether ThinkPHP uploads images to the database class
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.

  1. Class AttachModel extends Model {
  2. /**
  3. * Attachment Upload
  4. * @ Param string $ type: jpg, png
  5. * @ Param int $ maxsize maximum upload capacity: 100Kb by default
  6. * @ Param string $ module in which the model is uploaded
  7. * @ Param bool $ whether the insert statement is written to the database
  8. * @ Param bool $ whether thumb generates a thumbnail
  9. * @ Param string $ wh width and height of the thumbnail
  10. * Example: $ upload-> upload (null, 102400, APP_NAME, true, true, array ('20140901', '20160901 '));
  11. */
  12. Public function upload ($ type = null, $ maxsize = '000000', $ model = null, $ insert = true, $ thumb = false, $ wh = array ('000000 ', '20140901 ')){
  13. // Import Upload class
  14. Import ('org. NET. uploadfile ');
  15. $ Upload = new UploadFile ();
  16. $ Upload-> maxSize = $ maxsize;
  17. If ($ type ){
  18. $ Type = explode (',', $ type );
  19. $ Upload-> allowExts = $ type;
  20. } Else {
  21. $ Upload-> allowExts = array ('jpg ', 'PNG', 'GIF', 'jpeg ');
  22. }
  23. If ($ model ){
  24. $ Upload-> savePath = '/cms/tpl/Index/Public/Uploads/'. $ model .'/';
  25. } Else {
  26. $ Upload-> savePath = '/cms/tpl/Index/Public/Uploads /';
  27. }
  28. If ($ thumb ){
  29. $ Upload-> thumb = true;
  30. $ Upload-> thumbPrefix = 'zj _';
  31. $ Upload-> thumbMaxWidth = $ wh [0];
  32. $ Upload-> thumbMaxHeight = $ wh [1];
  33. }
  34. $ Upload-> saveRule = uniqid; // upload image naming rules
  35. If (! $ Upload-> upload ()){
  36. Return $ upload-> getErrorMsg ();
  37. } Else {
  38. $ Uploadlist = $ upload-> getUploadFileInfo ();
  39. }
  40. If ($ insert ){
  41. Return $ this-> _ insert ($ uploadlist );
  42. } Else {
  43. Return $ uploadlist;
  44. }
  45. }
  46. /*
  47. * The uploaded attachments are integrated into the data required by attach, stored in the table, and an array is returned.
  48. **/
  49. Private function _ insert ($ uploadlist ){
  50. $ J = count ($ uploadlist );
  51. $ V = array ();
  52. Foreach ($ uploadlist as $ key => $ value)
  53. {
  54. $ V [$ key] ['name'] = $ value ['name'];
  55. $ V [$ key] ['hashname'] = $ value ['savename'];
  56. $ V [$ key] ['savepath'] = substr ($ value ['savepath'], 2 );
  57. $ V [$ key] ['bsize'] = $ value ['size'];
  58. $ V [$ key] ['User _ id'] = $ _ SESSION [C ('User _ AUTH_KEY ')];
  59. $ V [$ key] ['create _ time'] = time ();
  60. $ V [$ key] ['model _ name'] = APP_NAME;
  61. $ This-> add ($ v [$ key]);
  62. If ($ this-> thumb)
  63. {
  64. $ V [$ key] ['prefix'] = $ this-> thumbPrefix;
  65. }
  66. $ V [$ key] ['id'] = M ('Attach ')-> getLastInsID ();
  67. }
  68. Return $ v;
  69. }
  70. }

  1. Drop table if exists 'zj _ attach ';
  2. Create table 'zj _ attach '(
  3. 'Id' int (10) not null auto_increment,
  4. 'Name' varchar (100) not null comment 'attachment name ',
  5. 'Hashname' varchar (100) default NULL,
  6. 'Status' tinyint (1) default '1' comment' attachment status {1: enabled, 0: Disabled }',
  7. 'Savepath 'varchar (100) default NULL comment' storage address ',
  8. 'Bsize' varchar (100) default NULL comment' attachment size ',
  9. 'Model _ name' varchar (50) default null comment 'Module ',
  10. 'User _ id' int (10) default NULL comment' Upload user ID ',
  11. 'Create _ time' int (10) default null comment 'upload time ',
  12. Primary key ('id ')
  13. ) ENGINE = InnoDB default charset = utf8;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.