Home  >  Article  >  Backend Development  >  ThinkPHP uploads whether the image is stored in the database class

ThinkPHP uploads whether the image is stored in the database class

WBOY
WBOYOriginal
2016-07-25 09:05:551013browse
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
  1. class AttachModel extends Model{
  2. /**
  3. * Attachment upload
  4. * @param string $type File type: jpg, png
  5. * @param int $maxsize Maximum upload capacity: Default 100Kb
  6. * @param string $model Upload module
  7. * @param bool $insert Whether to write Database
  8. * @param bool $thumb Whether to generate thumbnails
  9. * @param string $wh The width and height of the thumbnails
  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 the 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 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 for attach and stored in the table And return 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(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 to which it belongs',
  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;
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn