cakephp 3 图片上传
cakephp 3 image upload
我正在尝试通过 cakephp 3 上传图片。我已经尝试了下面在 cakephp 2 中工作的代码。现在在 cakephp 3 中我已经尝试了下面的代码
数据库字段
ALTER TABLE `users` ADD `avatar` VARCHAR(255) NOT NULL ;
然后在用户 add.ctp 我创建了下面的表格
<?= $this->Form->create($user,['type' => 'file']) ?>
<?= $this->Form->input('avatar',['type' => 'file']);?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
在用户模型中我添加了这个验证
$validator
->add('avatar', [
'uploadError' => [
'rule' => 'uploadError',
'message' => 'The cover image upload failed.',
'allowEmpty' => TRUE,
],
'mimeType' => [
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => TRUE,
],
'fileSize' => [
'rule' => array('fileSize', '<=', '1MB'),
'message' => 'Cover image must be less than 1MB.',
'allowEmpty' => TRUE,
],
'processCoverUpload' => [
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => TRUE,
],
]);
之后我发现了这个错误
ext/fileinfo is required for validating file mime types
ndm's 之后,我改了
;extension=php_fileinfo.dll
到
extension=php_fileinfo.dll
在 php.ini 文件中
然后这个错误就没有了。但是我发现的新错误
error : Method processCoverUpload does not exist
但是在下面的方法中我添加了
public function processCoverUpload($check = array()) {
if (!is_uploaded_file($check['avatar']['tmp_name'])) {
return FALSE;
}
if (!move_uploaded_file($check['avatar']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['avatar']['name'])) {
return FALSE;
}
$this->data[$this->alias]['avatar'] = 'uploads/'. $check['avatar']['name'];
return TRUE;
}
我不知道为什么方法 processCoverUpload 不存在。你能解释一下吗?
如评论中所述,CakePHP 3.x 现在需要文件信息扩展名来验证 MIME 类型。
http://php.net/manual/en/fileinfo.installation.php
发生另一个错误是因为您没有定义可以找到您的自定义方法的提供程序。您很可能已将该方法添加到 table class,因此在这种情况下,您应该使用 table
提供程序
'processCoverUpload' => [
'provider' => 'table', // <<<< there you go
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => TRUE,
],
默认提供程序定义为使用 \Cake\Validation\Validation
class。
附带说明,$check
不会包含字段名称作为键,验证方法将接收数据中键所持有的纯值。
此外 $this->data
已不存在,现在 3.x 中的情况有所不同。如果你想修改数据,那么你应该使用beforeMarshal
or beforeSave
events. However, with the latter you'll run into problems in case the upload field is using the same name as the column in the database which stores the file path, as this will cause the array to be marshalled to the type of the column, which is most probably a string type: https://github.com/cakephp/cakephp/issues/5998
另见
我正在尝试通过 cakephp 3 上传图片。我已经尝试了下面在 cakephp 2 中工作的代码。现在在 cakephp 3 中我已经尝试了下面的代码
数据库字段
ALTER TABLE `users` ADD `avatar` VARCHAR(255) NOT NULL ;
然后在用户 add.ctp 我创建了下面的表格
<?= $this->Form->create($user,['type' => 'file']) ?>
<?= $this->Form->input('avatar',['type' => 'file']);?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
在用户模型中我添加了这个验证
$validator
->add('avatar', [
'uploadError' => [
'rule' => 'uploadError',
'message' => 'The cover image upload failed.',
'allowEmpty' => TRUE,
],
'mimeType' => [
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => TRUE,
],
'fileSize' => [
'rule' => array('fileSize', '<=', '1MB'),
'message' => 'Cover image must be less than 1MB.',
'allowEmpty' => TRUE,
],
'processCoverUpload' => [
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => TRUE,
],
]);
之后我发现了这个错误
ext/fileinfo is required for validating file mime types
ndm's
;extension=php_fileinfo.dll
到
extension=php_fileinfo.dll
在 php.ini 文件中
然后这个错误就没有了。但是我发现的新错误
error : Method processCoverUpload does not exist
但是在下面的方法中我添加了
public function processCoverUpload($check = array()) {
if (!is_uploaded_file($check['avatar']['tmp_name'])) {
return FALSE;
}
if (!move_uploaded_file($check['avatar']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['avatar']['name'])) {
return FALSE;
}
$this->data[$this->alias]['avatar'] = 'uploads/'. $check['avatar']['name'];
return TRUE;
}
我不知道为什么方法 processCoverUpload 不存在。你能解释一下吗?
如评论中所述,CakePHP 3.x 现在需要文件信息扩展名来验证 MIME 类型。
http://php.net/manual/en/fileinfo.installation.php
发生另一个错误是因为您没有定义可以找到您的自定义方法的提供程序。您很可能已将该方法添加到 table class,因此在这种情况下,您应该使用 table
提供程序
'processCoverUpload' => [
'provider' => 'table', // <<<< there you go
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => TRUE,
],
默认提供程序定义为使用 \Cake\Validation\Validation
class。
附带说明,$check
不会包含字段名称作为键,验证方法将接收数据中键所持有的纯值。
此外 $this->data
已不存在,现在 3.x 中的情况有所不同。如果你想修改数据,那么你应该使用beforeMarshal
or beforeSave
events. However, with the latter you'll run into problems in case the upload field is using the same name as the column in the database which stores the file path, as this will cause the array to be marshalled to the type of the column, which is most probably a string type: https://github.com/cakephp/cakephp/issues/5998
另见