使用 CakePHP 创建文件上传,所有 $file 数组条目返回文件名的单个第一个字符

发布于 2024-10-12 08:37:30 字数 1615 浏览 1 评论 0原文

在我的视图文件 (add.ctp) 中使用以下代码:

<div class="resources form">
<?php echo $this->Form->create('Resource');?>
    <fieldset>
        <legend><?php __('Add Resource'); ?></legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('file', array('type' => 'file'));
        ...
    ?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
...

...以及控制器中的以下代码...

function add() {
    if (!empty($this->data)) {
        $this->Resource->create();
        if ($this->uploadFile() && $this->Resource->save($this->data)) {
            $this->Session->setFlash(__('The resource has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The resource could not be saved. Please, try again.', true));
        }
    }
    ....
}

...使用以下函数来测试该过程:

function uploadFile() {
        $file=$this->data['Resource']['file'];
        $this->data['Resource']['filename'] = 'failing with name:'.$file['name'].'/ file_mime: '.$file['filemime'];
    return true;
}

数据库始终填充为文件原始文件名,无论我尝试上传什么文件或尝试访问 $file 数组的哪些元素。例如,使用 test.gif 我得到:

“failing with name:t/ file_mime: t”

值得注意的是,这段代码是我将函数要执行的任务减少到最低限度以定位问题的结果。因此结果很奇怪。文件名的数据库列是这样建立的:

filename/varchar(200)/utf8_general_ci/NOT NULL

我是 CakePHP 的新手,完全不知道要寻找什么来完成这项工作,让我抓狂。任何帮助或指出正确的方向将不胜感激。

Using the following code in my view file (add.ctp):

<div class="resources form">
<?php echo $this->Form->create('Resource');?>
    <fieldset>
        <legend><?php __('Add Resource'); ?></legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('file', array('type' => 'file'));
        ...
    ?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
...

...and the following code in the controller...

function add() {
    if (!empty($this->data)) {
        $this->Resource->create();
        if ($this->uploadFile() && $this->Resource->save($this->data)) {
            $this->Session->setFlash(__('The resource has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The resource could not be saved. Please, try again.', true));
        }
    }
    ....
}

...with the following function to test the process:

function uploadFile() {
        $file=$this->data['Resource']['file'];
        $this->data['Resource']['filename'] = 'failing with name:'.$file['name'].'/ file_mime: '.$file['filemime'];
    return true;
}

The database is always populated with the first character of the files original filename, regardless of what file I try to upload or what elements of the $file array I try to access. For example with test.gif I get:

"failing with name:t/ file_mime: t"

Worthy of note, this code is the result of me reducing the tasks the function was to perform down to the barest minimum to locate the problem. Hence the weirdness of the result. The database column for the filename is established like this:

filename/varchar(200)/utf8_general_ci/NOT NULL

I'm new to CakePHP and am at a total loss of what to look for to make this work, tearing my hair out. Any help or pointing in the right direction to look would be gratefully received.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-10-19 08:37:30

我设法找到了 CakePHP 的一位贡献者,他非常非常友善地提供了帮助并为我解决了问题,我非常感激,但会让他匿名!

乍一看,我可以看到您的 add.ctp 在表单创建中需要一个数组:

echo $this->Form->create('Resource', array('type' => 'file'));

这告诉 CakePHP 表单助手创建一个多部分表单类型,准备发送文件。

I managed to get hold of one of the contributors to CakePHP who very, very kindly offered his help and solved the issue for me, I am very grateful but will keep him anonymous!

At a glance, I can see that your add.ctp needs an array in the form creation:

echo $this->Form->create('Resource', array('type' => 'file'));

This tells the CakePHP form helper to create a multi-part form type, ready for sending files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文