cakePHP 3.0上传图像
我想在我的cakephp 3.0应用程序中上传图像。但我得到了错误信息:cakePHP 3.0上传图像,php,cakephp,file-upload,Php,Cakephp,File Upload,我想在我的cakephp 3.0应用程序中上传图像。但我得到了错误信息: Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55] 在cakePHP 3.0中已经有一些上传文件(一次多个文件)的例子了吗?因为我只能找到CakePHP2.x的例子 我想我需要在ImagesTable.php中添加一个自定义验证方法?但我不能让它工作 ImagesTable public function i
Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55]
在cakePHP 3.0中已经有一些上传文件(一次多个文件)的例子了吗?因为我只能找到CakePHP2.x的例子
我想我需要在ImagesTable.php中添加一个自定义验证方法?但我不能让它工作
ImagesTable
public function initialize(array $config) {
$validator
->requirePresence('image_path', 'create')
->notEmpty('image_path')
->add('processImageUpload', 'custom', [
'rule' => 'processImageUpload'
])
}
public function processImageUpload($check = array()) {
if(!is_uploaded_file($check['image_path']['tmp_name'])){
return FALSE;
}
if (!move_uploaded_file($check['image_path']['tmp_name'], WWW_ROOT . 'img' . DS . 'images' . DS . $check['image_path']['name'])){
return FALSE;
}
$this->data[$this->alias]['image_path'] = 'images' . DS . $check['image_path']['name'];
return TRUE;
}
图像控制器
public function add()
{
$image = $this->Images->newEntity();
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
$data = $this->request->data['Images'];
//var_dump($this->request->data);
if(!$data['image_path']['name']){
unset($data['image_path']);
}
// var_dump($this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success('The image has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The image could not be saved. Please, try again.');
}
}
$images = $this->Images->Images->find('list', ['limit' => 200]);
$projects = $this->Images->Projects->find('list', ['limit' => 200]);
$this->set(compact('image', 'images', 'projects'));
$this->set('_serialize', ['image']);
}
if (!empty($this->request->data)) {
if (!empty($this->request->data['upload']['name'])) {
$file = $this->request->data['upload']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
$setNewFileName = time() . "_" . rand(000000, 999999);
//only process if the extension is valid
if (in_array($ext, $arr_ext)) {
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . '/upload/avatar/' . $setNewFileName . '.' . $ext);
//prepare the filename for database entry
$imageFileName = $setNewFileName . '.' . $ext;
}
}
$getFormvalue = $this->Users->patchEntity($particularRecord, $this->request->data);
if (!empty($this->request->data['upload']['name'])) {
$getFormvalue->avatar = $imageFileName;
}
if ($this->Users->save($getFormvalue)) {
$this->Flash->success('Your profile has been sucessfully updated.');
return $this->redirect(['controller' => 'Users', 'action' => 'dashboard']);
} else {
$this->Flash->error('Records not be saved. Please, try again.');
}
}
图像添加.ctp
<?php
echo $this->Form->input('image_path', [
'label' => 'Image',
'type' => 'file'
]
);
?>
<div class="ChImg">
<?php
echo $this->Form->create($particularRecord, ['enctype' => 'multipart/form-data']);
echo $this->Form->input('upload', ['type' => 'file']);
echo $this->Form->button('Update Details', ['class' => 'btn btn-lg btn-success1 btn-block padding-t-b-15']);
echo $this->Form->end();
?>
</div>
也许下面的内容会有所帮助。这是一个行为谁帮助你上传文件非常容易 如果你挣扎,请告诉我
格里茨既然每个人都在这里为自己的插件做广告,那我也来做吧。我已经检查了另一个问题中链接的可上传行为,它非常简单,看起来已经完成了一半 如果您想要一个可在企业级扩展的完整解决方案,请查看。它有一些我在其他任何实现中都没有见过的特性,比如在获得大量文件的情况下,确保不会遇到文件系统限制。它与一起处理图像。可以单独使用或组合使用,如下所示 它完全基于事件,您可以通过实现自己的事件侦听器来更改一切。这将需要一些中级的CakePHP经验 有一种方法可以看到实现它有多容易。下面的代码取自它,这是一个完整的示例,请参阅《快速入门指南》,它更详细
class Products extends Table {
public function initialize() {
parent::initialize();
$this->hasMany('Images', [
'className' => 'ProductImages',
'foreignKey' => 'foreign_key',
'conditions' => [
'Documents.model' => 'ProductImage'
]
]);
$this->hasMany('Documents', [
'className' => 'FileStorage.FileStorage',
'foreignKey' => 'foreign_key',
'conditions' => [
'Documents.model' => 'ProductDocument'
]
]);
}
}
class ProductsController extends ApController {
// Upload an image
public function upload($productId = null) {
if (!$this->request->is('get')) {
if ($this->Products->Images->upload($productId, $this->request->data)) {
$this->Session->set(__('Upload successful!');
}
}
}
}
class ProductImagesTable extends ImageStorageTable {
public function uploadImage($productId, $data) {
$data['adapter'] = 'Local';
$data['model'] = 'ProductImage',
$data['foreign_key'] = $productId;
$entity = $this->newEntity($data);
return $this->save($data);
}
public function uploadDocument($productId, $data) {
$data['adapter'] = 'Local';
$data['model'] = 'ProductDocument',
$data['foreign_key'] = $productId;
$entity = $this->newEntity($data);
return $this->save($data);
}
}
在视图文件中,像这样添加,在我的例子中是用户/dashboard.ctp
<?php
echo $this->Form->input('image_path', [
'label' => 'Image',
'type' => 'file'
]
);
?>
<div class="ChImg">
<?php
echo $this->Form->create($particularRecord, ['enctype' => 'multipart/form-data']);
echo $this->Form->input('upload', ['type' => 'file']);
echo $this->Form->button('Update Details', ['class' => 'btn btn-lg btn-success1 btn-block padding-t-b-15']);
echo $this->Form->end();
?>
</div>
使用前,在webroot中创建一个名为upload/avatar的文件夹
注意:输入('Name Here')用于
$this->request->data['upload']['name']
如果要查看数组结果,可以打印它
它就像CakePHP 3.x中的一个魅力,我们在制作应用程序中使用它并取得了巨大成功,并且已经使用了相当长的一段时间
对使用“Flysystem”(也就是从特定文件系统中抽象出来的)有极好的支持,所以从普通的本地文件系统移动到S3是一件很简单的事情,或者是Dropbox,或者任何你想要的地方:-)
你可以在这里找到相关的(高质量)文件上传插件:-我也成功地使用了“Proffer”,它绝不是“几乎完成”或任何类似的东西-都有我所有的建议,并且在我看来已经准备好了 嘿,伙计,帮帮我?我已经安装了上面的程序,完全按照文档中的说明操作,但它没有上传文件或将文件名存储到数据库中。有什么想法吗?@SyamsoulAzrien你应该问一个新问题:-)链接断了。老兄,我试过你的代码,图像上传效果很好。但问题是现在没有保存其他字段数据。为什么会这样??我是否需要进行任何更改以确保正确保存???$this->request->data应返回表单数据“name”,例如,电子邮件文本框中应包含表格名称中的电子邮件。其余的也一样。无需任何更改。@PrassannaD无法打开流:没有此类文件或目录移动\u上载\u文件()[]”rel=“nofollow noreferrer”>php.net/…:无法move@IdhamChoudry文件夹在您提到的位置不存在,或者可能没有文件夹权限。
$this->request->data['upload']['name']