i think this is a bug within Cakephp 3 but i wanted to double check first.
Cakephp 3.2.3 - Windows 7 with XAMPP 3.2.2
View: file.ctp
<div>
<?= $this->Form->create($client, ['type' => 'file']) ?>
<?= $this->Form->input('logo', ['type' => 'file']); ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
Controller: 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']);
}
Model: 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');
}
This is a simple ImageUpload-Test script which i tried over the last days. The mimeType validation causes an exception if the filesize is above the upload boundaries. I don't mean the fileSize validation amount. I mean the value which is mentioned within PHP.ini (in my case upload_max_filesize=2M).
If i upload a file which is bigger than 2MB, i get this exception
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}
the value within $this->request->data
is:
Array
(
[logo] => Array
(
[name] => Image6MB.jpg
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
This case always throws the exception if mimeType check is within the validation variable.
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');
}
This should be test case within the cake core tests. As the apache did not upload the data and set the error
to 1. This should be handled by the code and not throw an exception.
I have 3 validation checks and anyhow end up with an exception. The uploadError
can't even be displayed as the exception is raised. mimeType check and fileSize check can only be done if the file was uploaded successfully.
How should this case be handled?