【问题标题】:test file upload with cakephp使用 cakephp 测试文件上传
【发布时间】: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


【解决方案1】:

Heureka!!!

我在大约 4 天后找到了解决方案!

问题出在 TemplateDownloads/add.ctp Cake 不喜欢 Form-Control-command 中的'Download.name' 它必须是没有下载的唯一名称(它已在 Form-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

【讨论】:

    猜你喜欢
    • 2019-06-14
    • 2019-04-30
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多