使用 CakePHP 创建文件上传,所有 $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>
...
...以及控制器中的以下代码...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法找到了 CakePHP 的一位贡献者,他非常非常友善地提供了帮助并为我解决了问题,我非常感激,但会让他匿名!
乍一看,我可以看到您的 add.ctp 在表单创建中需要一个数组:
这告诉 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:
This tells the CakePHP form helper to create a multi-part form type, ready for sending files.