0
点赞
收藏
分享

微信扫一扫

CakePHP中使用MeioUpload Behavior上传图片[转]


      这款MeioUpload Behavior​ 真是帮我解决了大问题,感谢作者​ 和阿辉​ ,另外CakePHP的app/models/behaviors目录是专门用来存放相关行为处理文件的,大家如果想省事儿,可以到http://bakery.cakephp.org/​ 来先找找有没有人事先写好的代码,记录下图片上传先。 我的文章表里有两个字段:thumbnailimg 和 largeimg ,分别代表小图和大图,文章添加时上传的两张图片保存到webroot/files/images下,并把路径和文件名分别保存到这两个字段,实现过程如下:

  1. 下载MeioUpload 并把解压后的文件放到app/models/behaviors目录下。
  2. 将下面代码加入到Article Model中。

var

$actsAs
=
array


(


// models/behaviors/meio_upload.php


'MeioUpload'
=>
array



(

'thumbnailimg'
=>
array


(

'dir'
=>
'files{DS}images'
,


'create_directory'
=>
true
,


'allowed_mime'
=>
array


(

'image/jpeg'
,

'image/pjpeg'
,

'image/gif'
,

'image/png'

)
,


'allowed_ext'
=>
array



(

'.jpg'
,

'.jpeg'
,

'.png'
,

'.gif'

)


)
,


'largeimg'
=>
array


(


'dir'
=>
'files{DS}images'
,


'create_directory'
=>
true
,


'allowed_mime'
=>
array


(

'image/jpeg'
,

'image/pjpeg'
,

'image/gif'
,

'image/png'

)
,


'allowed_ext'
=>
array


(

'.jpg'
,

'.jpeg'
,

'.png'
,

'.gif'

)


)


)


)
;

  1. 在文章添加view模版相应位置分别放上以下两段代码用来提交图片。

<
?php
echo
$form
->
input
(
'largeimg'
,
array

(
'type'
=>
'file'
,
'label'
=>
false
,
'error'
=>
'请选择大图'
)
)
;

?>

<
?php
echo
$form
->
input
(
'Thumbnailimg'
,
array

(
'type'
=>
'file'
,
'label'
=>
false
,
'style'
=>
'display:none;'
)
)
;

?>

  1. 注意事项:
  • php.ini中的MAX_FILE_SIZE默认值是2M,如果你想上传更大的文件,应该把这里的Size改大一点。
  • 在view模版中添加的这两个提交图片的默认是不能为空的,如果想设置可以为空,假如想设置小图上传可以为空,就可以在Article Model中设置一下验证规则:

public
$validate
=
array

(

'Thumbnailimg'
=>
array

(

'Empty'
=>
array

(
'check'
=>
false
)

)


)
;

参考:​​http://www.meiocodigo.com/projects/meioupload/​​


举报

相关推荐

0 条评论