thinkphp upload a picture whether to save the database class

Source: Internet
Author: User
Look at the project requirements: You can choose whether to save 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 file type: jpg,png
  5. * @param int $maxsize max upload capacity: Default 100Kb
  6. * @param string $model upload module
  7. * @param bool $insert write to Database
  8. * @param bool $thumb whether to generate thumbnails
  9. * @param string $wh The width of the thumbnail
  10. * Example: $upload->upload (Null,102400,app_name,true,true,array (' 300 ', ' 225 '));
  11. */
  12. Public function upload ($type = null, $maxsize = ' 102400 ', $model = null, $insert = True, $thumb = False, $wh = Array (' 160 ') , ' 120 ')) {
  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 = '. /public/uploads/'. $model. ' /';
  25. }else{
  26. $upload->savepath = '. /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 picture 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. * uploaded attachments are consolidated into attach data, deposited into the table and returned to the array
  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. }
Copy Code
    1. DROP TABLE IF EXISTS ' Zj_attach ';
    2. CREATE TABLE ' Zj_attach ' (
    3. ' id ' int (ten) is not NULL auto_increment,
    4. ' Name ' varchar (+) not NULL COMMENT ' attachment name ',
    5. ' hashname ' varchar default NULL,
    6. ' Status ' tinyint (1) Default ' 1 ' COMMENT ' attachment status {1: enabled, 0: disabled} ',
    7. ' Savepath ' varchar (+) default NULL COMMENT ' storage address ',
    8. ' bsize ' varchar (+) Default NULL COMMENT ' attachment size ',
    9. ' model_name ' varchar default NULL COMMENT ' belongs to module ',
    10. ' user_id ' int (ten) default NULL COMMENT ' upload user ID ',
    11. ' Create_time ' int (ten) default NULL COMMENT ' upload time ',
    12. PRIMARY KEY (' id ')
    13. ) Engine=innodb DEFAULT Charset=utf8;
Copy Code
  • 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.