【发布时间】:2017-10-19 15:50:57
【问题描述】:
是否可以在您自己的计算机上使用模拟服务器测试文件上传,还是只能在真实服务器上测试???
如果我尝试上传文件,我会收到消息“无法保存下载。请再试一次。'。 (消息来自我自己的 DownloadController。)没有任何内容保存到上传路径或存储在数据库中。
所以 CakePHP 运行一个内置的 Web 服务器。我想我可以将文件上传到我自己的计算机(我知道这不是真正的上传,但有我的文件、网络服务器、带有上传路径的 webroot ......) 但也许我尝试这个完全错了???真的在网上找不到任何东西......
在我的应用程序中,我想让用户有机会下载文件。 所以我已经做了什么。
安装了上传插件 Josegonzalez。
下载表格.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'}
【问题讨论】:
-
请始终提及您的确切 CakePHP 版本(vendor/cakephp/cakephp/VERSION.txt 或 lib/Cake/VERSION.txt 中的最后一行)
-
1.不用传
file_dir, 2. 也可以在保存后调试你的实体,并显示之前和之后的结果。
标签: php cakephp testing file-upload