我正在开发CakePHP 3项目并使用Proffer插件上传图片。这个插件可以正常上传单个图像。
现在我希望能够上传多张图片。
我将图片存储到service_category_images
表格中,该表格与service_categories
与外键service_category_id
相关。
按照Proffer插件指南中的说明上传多个图像。
我的ServiceCategoryImagesTable.php
文件
<?php
namespace App\Model\Table;
use App\Model\Entity\ServiceCategoryImage;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* ServiceCategoryImages Model
*
* @property \Cake\ORM\Association\BelongsTo $ServiceCategories
*/
class ServiceCategoryImagesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('service_category_images');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Proffer.Proffer', [
'image' => [
'root' => DS . 'var' . DS . 'www' . DS . 'html' . DS . 'flitfix' . DS . 'media' . DS . 'files',
'dir' => 'dir'
]
]);
$this->belongsTo('ServiceCategories', [
'foreignKey' => 'service_category_id',
'joinType' => 'INNER'
]);
}
public function validationDefault(Validator $validator)
{
$validator->provider('proffer', 'Proffer\Model\Validation\ProfferRules');
$validator
->uuid('id')
->allowEmpty('id', 'create');
$validator
->add('image', 'proffer', [
'rule' => ['dimensions', [
'min' => ['w' => '100', 'h' => '100'],
'max' => ['w' => '1000', 'h' => '1000']
]],
'message' => 'Please upload image of correct dimension',
'provider' => 'proffer'
])
->requirePresence('image', 'create')
->allowEmpty('image');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['service_category_id'], 'ServiceCategories'));
return $rules;
}
}
和ServiceCategoryImage.php
实体包含
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* ServiceCategoryImage Entity.
*
* @property string $id
* @property string $service_category_id
* @property \App\Model\Entity\ServiceCategory $service_category
* @property string $image_01
* @property string $image_02
* @property string $image_03
* @property string $image_04
* @property string $dir
* @property \Cake\I18n\Time $created
* @property \Cake\I18n\Time $modified
*/
class ServiceCategoryImage extends Entity
{
protected $_accessible = [
'*' => true,
'id' => false,
'service_category_images' => true,
];
}
add.ctp
ServiceCategoryImages
控制器视图
<div class="serviceCategoryImages form large-9 medium-8 columns content">
<?= $this->Form->create($serviceCategoryImage, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Service Category Image') ?></legend>
<?php
echo $this->Form->input('service_category_id', ['options' => $serviceCategories]);
echo $this->Form->input('service_category_images.0.image', ['type' => 'file']);
echo $this->Form->input('service_category_images.1.image', ['type' => 'file']);
echo $this->Form->input('service_category_images.2.image', ['type' => 'file']);
echo $this->Form->input('service_category_images.3.image', ['type' => 'file']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
和ServiceCategoriesTable.php
有很多关系
$this->hasMany('ServiceCategoryImages', [
'foreignKey' => 'service_category_id'
]);
但是,它不是上传文件。有什么不对吗?
编辑2:error.log内容
2016-06-28 06:57:30 Warning: Warning (2): getimagesize() [<a href='http://php.net/function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in [/var/www/html/flitfix/admin/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29]
Request URL: /service-category-images/add
Referer URL: http://admin.flitfix2.com/service-category-images/add
Client IP: 127.0.0.1
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
getimagesize - [internal], line ??
Proffer\Model\Validation\ProfferRules::dimensions() - ROOT/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29
Cake\Validation\ValidationRule::process() - CORE/src/Validation/ValidationRule.php, line 138
Cake\Validation\Validator::_processRules() - CORE/src/Validation/Validator.php, line 1505
Cake\Validation\Validator::errors() - CORE/src/Validation/Validator.php, line 137
Cake\ORM\Marshaller::_validate() - CORE/src/ORM/Marshaller.php, line 203
Cake\ORM\Marshaller::merge() - CORE/src/ORM/Marshaller.php, line 488
Cake\ORM\Table::patchEntity() - CORE/src/ORM/Table.php, line 2099
App\Controller\ServiceCategoryImagesController::add() - APP/Controller/ServiceCategoryImagesController.php, line 56
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 429
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 36
debug.log内容
2016-06-28 06:57:30 Notice: Notice (8): Undefined index: tmp_name in [/var/www/html/flitfix/admin/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29]
Request URL: /service-category-images/add
Referer URL: http://admin.flitfix2.com/service-category-images/add
Client IP: 127.0.0.1
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
Proffer\Model\Validation\ProfferRules::dimensions() - ROOT/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29
Cake\Validation\ValidationRule::process() - CORE/src/Validation/ValidationRule.php, line 138
Cake\Validation\Validator::_processRules() - CORE/src/Validation/Validator.php, line 1505
Cake\Validation\Validator::errors() - CORE/src/Validation/Validator.php, line 137
Cake\ORM\Marshaller::_validate() - CORE/src/ORM/Marshaller.php, line 203
Cake\ORM\Marshaller::merge() - CORE/src/ORM/Marshaller.php, line 488
Cake\ORM\Table::patchEntity() - CORE/src/ORM/Table.php, line 2099
App\Controller\ServiceCategoryImagesController::add() - APP/Controller/ServiceCategoryImagesController.php, line 56
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 429
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 36