是否可以使用模拟服务器在您自己的计算机上测试文件上传,或者它只能用于真实服务器?
如果我尝试上传文件,则收到消息'无法保存下载内容。请再试一次。'。 (该消息来自我自己的DownloadController。)没有任何内容保存到upload-path或存储在数据库中。
所以CakePHP运行内置的Web服务器。我可以将文件上传到我自己的计算机上(我知道它不是真正的上传,但有我的文件,网络服务器,带上传路径的webroot ......) 但也许我尝试这个完全错了?真的没有在网上找到任何东西......
在我的应用程序中,我想让用户有机会下载文件。 那我还做了什么。
安装了上传插件Josegonzalez。
DownloadsTables.php
$this->addBehavior('Josegonzalez/Upload.Upload', [
'file' => [
'path' => 'webroot{DS}downloads{DS}',
'fields' => [
// if these fields or their defaults exists
// the values will be set.
'dir' => 'file_dir', //defaults to 'dir'
'size' => 'file_size', //defaults to 'size'
'type' => 'file_type', //defaults to 'type'
],
],
]);
在TemplateDownloads / add.ctp
中<?= $this->Form->create('Download', ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Download') ?></legend>
<?php
echo $this->Form->control('Download.name');
echo $this->Form->control('Download.file', ['type' => 'file']);
echo $this->Form->control('Download.file_dir', ['type' => 'hidden']);
//echo $this->Form->control('meetings._ids', ['options' => $meetings]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
在DownloadsController.php中
public function add()
{
$download = $this->Downloads->newEntity();
// @ToDo download auf Server aktivieren
if ($this->request->is('post')) {
//debug ($this->request);
//debug ($this->Downloads);
$download = $this->Downloads->patchEntity($download, $this->request->getData());
debug ($download);
debug ($this->addBehavior);
if ($this->Downloads->save($download)) {
$this->Flash->success(__('The download has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The download could not be saved. Please, try again.'));
}
$meetings = $this->Downloads->Meetings->find('list', ['limit' => 200]);
$this->set(compact('download', 'meetings'));
$this->set('_serialize', ['download']);
}
bootstrap.php中
Plugin::load('Josegonzalez/Upload');
所以我很期待你的帮助,并感谢你的建议
编辑:
我的CakePHP版本是3.4.6
保存前的实体
object(App\Model\Entity\Download) {
'Download' => [
'name' => 'test1',
'file_dir' => '',
'file' => [
'tmp_name' => '/tmp/phpiBp9GI',
'error' => (int) 0,
'name' => 'test1',
'type' => 'application/octet-stream',
'size' => (int) 15
]
],
'[new]' => true,
'[accessible]' => [
'*' => true,
'id' => false
],
'[dirty]' => [
'Download' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Downloads'}
并保存后
object(App\Model\Entity\Download) {
'Download' => [
'name' => 'test1',
'file_dir' => '',
'file' => [
'tmp_name' => '/tmp/phpiBp9GI',
'error' => (int) 0,
'name' => 'test1',
'type' => 'application/octet-stream',
'size' => (int) 15
]
],
'file' => null,
'[new]' => true,
'[accessible]' => [
'*' => true,
'id' => false
],
'[dirty]' => [
'Download' => true,
'file' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Downloads'}
答案 0 :(得分:0)
Heureka !!!
我在大约4天后找到了解决方案!
问题出在TemplateDownloads / add.ctp中 Cake不喜欢Form-Control-command'Download.name' 它必须是没有下载的名称(它'已经在theForm-create-command中声明!
所以正确的TemplateDownloads / app.ctp
<div class="downloads form large-9 medium-8 columns content">
<?= $this->Form->create('Download', ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Download') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('file', ['type' => 'file']);
echo $this->Form->control('file_dir', ['type' => 'hidden']);
echo $this->Form->control('meetings._ids', ['options' => $meetings]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
因此可以测试lokal文件上传!!与CakePHP