CakePHP Burzum/file-storage 无法保存文件
CakePHP Burzum/file-storage not able to save files
我安装了 "burzum/file-storage": "^1.1"
并遵循了文档
将此添加到 bootstrap.php:
use Cake\Event\EventManager;
use Burzum\FileStorage\FileStorageUtils;
use Burzum\FileStorage\Storage\StorageManager;
use Burzum\FileStorage\Event\LocalFileStorageListener;
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem'
]);
$listener = new LocalFileStorageListener();
EventManager::instance()->on($listener);
我有一个名为 Comments 的 table 并从文档
添加了它
$this->hasOne('PdfFiles', [
'className' => 'Burzum/FileStorage.PdfFiles',
'foreignKey' => 'foreign_key',
'conditions' => [
'PdfFiles.model' => 'Comments'
]
]);
添加了这个表格:
<?= $this->Form->create($comment, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Edit Comment') ?></legend>
<?php
echo $this->Form->file('pdf_files.file');
echo $this->Form->input('comment');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
使用
将 table 添加到我的数据库
cake migrations migrate -p Burzum/FileStorage
当我发送表格时,文件不会保存在 TMP 文件夹中,也不会保存在数据库中。
我尽量坚持事件。我不想使用手动版本(顺便说一句,这很有效)。
我错过了什么?
此致
桑迪法
如果您仍然需要这方面的帮助,我会详细介绍。
您需要创建另一个 table 我的是 articles 图片您的可能是 CommentDocs
<?php
namespace App\Model\Table;
use Burzum\FileStorage\Model\Table\ImageStorageTable;
class ArticleImagesTable extends ImageStorageTable {
public $actsAs = array(
'FileStorage.UploadValidator' => array(
'allowedExtensions' => array(
'jpg',
'jpeg',
'png'
),
),
);
public function upload($article_id, $entity) {
$entity = $this->patchEntity($entity, [
'adapter' => 'Local',
'model' => 'Articles',
'foreign_key' => $article_id,
]);
return $this->save($entity);
}
}
?>
//在我的文章中table我有
$this->hasMany('ArticleImages', [
'foreignKey' => 'foreign_key',
'conditions' => [
'model' => 'Articles'
]
]);
//我的文章控制器
public function upload($id = null) {
$entity = $this->Articles->ArticleImages->newEntity();
if ($this->request->is(['post', 'put'])) {
$entity = $this->Articles->ArticleImages->patchEntity($entity,$this->request->data);
if ($this->Articles->ArticleImages->upload($id, $entity)) {
$this->Flash->set(__('Upload successful!'));
}
}
$this->set('ArticleImage', $entity);
}
我安装了 "burzum/file-storage": "^1.1"
并遵循了文档
将此添加到 bootstrap.php:
use Cake\Event\EventManager;
use Burzum\FileStorage\FileStorageUtils;
use Burzum\FileStorage\Storage\StorageManager;
use Burzum\FileStorage\Event\LocalFileStorageListener;
StorageManager::config('Local', [
'adapterOptions' => [TMP, true],
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem'
]);
$listener = new LocalFileStorageListener();
EventManager::instance()->on($listener);
我有一个名为 Comments 的 table 并从文档
添加了它$this->hasOne('PdfFiles', [
'className' => 'Burzum/FileStorage.PdfFiles',
'foreignKey' => 'foreign_key',
'conditions' => [
'PdfFiles.model' => 'Comments'
]
]);
添加了这个表格:
<?= $this->Form->create($comment, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Edit Comment') ?></legend>
<?php
echo $this->Form->file('pdf_files.file');
echo $this->Form->input('comment');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
使用
将 table 添加到我的数据库cake migrations migrate -p Burzum/FileStorage
当我发送表格时,文件不会保存在 TMP 文件夹中,也不会保存在数据库中。
我尽量坚持事件。我不想使用手动版本(顺便说一句,这很有效)。
我错过了什么?
此致 桑迪法
如果您仍然需要这方面的帮助,我会详细介绍。
您需要创建另一个 table 我的是 articles 图片您的可能是 CommentDocs
<?php
namespace App\Model\Table;
use Burzum\FileStorage\Model\Table\ImageStorageTable;
class ArticleImagesTable extends ImageStorageTable {
public $actsAs = array(
'FileStorage.UploadValidator' => array(
'allowedExtensions' => array(
'jpg',
'jpeg',
'png'
),
),
);
public function upload($article_id, $entity) {
$entity = $this->patchEntity($entity, [
'adapter' => 'Local',
'model' => 'Articles',
'foreign_key' => $article_id,
]);
return $this->save($entity);
}
}
?>
//在我的文章中table我有
$this->hasMany('ArticleImages', [
'foreignKey' => 'foreign_key',
'conditions' => [
'model' => 'Articles'
]
]);
//我的文章控制器
public function upload($id = null) {
$entity = $this->Articles->ArticleImages->newEntity();
if ($this->request->is(['post', 'put'])) {
$entity = $this->Articles->ArticleImages->patchEntity($entity,$this->request->data);
if ($this->Articles->ArticleImages->upload($id, $entity)) {
$this->Flash->set(__('Upload successful!'));
}
}
$this->set('ArticleImage', $entity);
}