【发布时间】:2016-06-08 18:18:56
【问题描述】:
我认为这是 Cakephp 3 中的一个错误,但我想先仔细检查一下。
Cakephp 3.2.3 - 带有 XAMPP 3.2.2 的 Windows 7
查看:file.ctp
<div>
<?= $this->Form->create($client, ['type' => 'file']) ?>
<?= $this->Form->input('logo', ['type' => 'file']); ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
控制器:ClientsController.php
public function file() {
$client = $this->Clients->newEntity();
if ($this->request->is('post') ) {
$client = $this->Clients->patchEntity($client, $this->request->data);
if (!is_null($client->logo)
&& !empty($this->request->data)
&& !empty($this->request->data['logo'])
&& !empty($this->request->data['logo']['name']))
$client->logo = $this->request->data['logo'];
$client->name = 'TEST';
if ($this->Clients->save($client)) {
$this->Flash->success(__('The client has been saved.'));
return $this->redirect(['action' => 'file']);
} else {
$this->Flash->error(__('The client could not be saved. Please, try again.'));
}
}
$this->set(compact('client'));
$this->set('_serialize', ['client']);
}
模型:ClientsTable.php
public function validationDefault(Validator $validator)
{
$validator
->add('logo', [
'uploadError' => [
'rule' => 'uploadError',
'message' => __d('clients', 'The logo upload failed.')
],
'mimeType' => [
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => __d('clients', 'Please upload images only (gif, png, jpg).')
],
'fileSize' => [
'rule' => array('fileSize', '<=', '1MB'),
'message' => __d('clients', 'Logo image must be less than 1MB.')
],
])
->allowEmpty('logo');
}
这是一个简单的 ImageUpload-Test 脚本,我在过去几天尝试过。 如果文件大小超过上传边界,则 mimeType 验证会导致异常。我不是指 fileSize 验证量。我的意思是 PHP.ini 中提到的值(在我的例子中是 upload_max_filesize=2M)。
如果我上传的文件大于 2MB,我会收到此异常
2016-02-25 18:33:56 Error: [RuntimeException] Cannot validate mimetype for a missing file
Request URL: /pam/clients/file
Referer URL: http://localhost/pam/clients/file
Stack Trace:
#0 [internal function]: Cake\Validation\Validation::mimeType(Array, Array)
#1 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Validation\RulesProvider.php(71): ReflectionMethod->invokeArgs(NULL, Array)
#2 [internal function]: Cake\Validation\RulesProvider->__call('mimeType', Array)
#3 [internal function]: Cake\Validation\RulesProvider->mimeType(Array, Array, Array)
#4 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Validation\ValidationRule.php(138): call_user_func_array(Array, Array)
#5 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Validation\Validator.php(1410): Cake\Validation\ValidationRule->process(Array, Array, Array)
#6 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Validation\Validator.php(137): Cake\Validation\Validator->_processRules('logo', Object(Cake\Validation\ValidationSet), Array, true)
#7 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\ORM\Marshaller.php(193): Cake\Validation\Validator->errors(Array, true)
#8 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\ORM\Marshaller.php(466): Cake\ORM\Marshaller->_validate(Array, Array, true)
#9 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\ORM\Table.php(2073): Cake\ORM\Marshaller->merge(Object(App\Model\Entity\Client), Array, Array)
#10 C:\Users\D052192\OneDrive\xampp\htdocs\pam\src\Controller\ClientsController.php(102): Cake\ORM\Table->patchEntity(Object(App\Model\Entity\Client), Array)
#11 [internal function]: App\Controller\ClientsController->file()
#12 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\friendsofcake\crud\src\Controller\ControllerTrait.php(51): call_user_func_array(Array, Array)
#13 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(114): App\Controller\AppController->invokeAction()
#14 C:\Users\D052192\OneDrive\xampp\htdocs\pam\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\ClientsController))
#15 C:\Users\D052192\OneDrive\xampp\htdocs\pam\webroot\index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#16 {main}
$this->request->data 中的值为:
Array
(
[logo] => Array
(
[name] => Image6MB.jpg
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
如果 mimeType 检查在验证变量内,这种情况总是会引发异常。
public static function mimeType($check, $mimeTypes = [])
{
if (is_array($check) && isset($check['tmp_name'])) {
$check = $check['tmp_name'];
}
if (!function_exists('finfo_open')) {
throw new LogicException('ext/fileinfo is required for validating file mime types');
}
if (!is_file($check)) {
throw new RuntimeException('Cannot validate mimetype for a missing file');
}
这应该是蛋糕核心测试中的测试用例。由于apache没有上传数据并将error设置为1。这应该由代码处理,不要抛出异常。
我进行了 3 次验证检查,但最终还是出现了异常。 uploadError 甚至无法显示,因为引发了异常。 mimeType 检查和 fileSize 检查只有在文件成功上传的情况下才能进行。
这种情况应该如何处理?
【问题讨论】:
标签: php validation cakephp