[英]test file upload with cakephp
是否可以使用模拟服务器在您自己的计算机上测试文件上传,还是只能在真实服务器上进行?
如果我尝试上传文件,则会收到消息“无法保存下载。 请再试一次。'。 (消息来自我自己的DownloadController。)没有任何内容保存到上载路径或存储在数据库中。
因此CakePHP运行一个内置的Web服务器。 我可以将文件上传到我自己的计算机上(我知道它不是真正的上传,但是有我的文件,网络服务器,带有上载路径的网络根...),但是尝试这样做可能完全错误??? 真的在网上找不到任何东西...
在我的应用程序中,我希望为用户提供下载文件的机会。 所以我已经做了。
安装了上传插件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'}
Heureka!
大约4天后我找到了解决方案!
问题出在TemplateDownloads / add.ctp蛋糕上,它不像Form-Control命令中的“ Download.name”那样。它只能是没有下载的名称(在Form-create-command中声明为allready!
所以正确的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.