我正在尝试修改一个用CakePHP编写的时事通讯平台,以制作附件。在创建时事通讯时,可以上传一个图像文件,该文件将作为附件添加。但是,在表单提交(新闻稿创建是一个表单)之后,$_FILES变量是空的。但是文件的名称包含在POST数据中。
以下是一些HTML表单
<form action='/systeem/nieuwsbrieven/' method='POST'>
<tr>
<td style='padding: 10px;'><textarea name='data[Nieuwsbrieven][omschrijving]' style='width: 100%;height: 150px;'></textarea></td>
</tr>
<tr>
<td style='padding: 10px;'><input type="file" id='fileupload' name="data[Nieuwsbrieven][attachment]" style='width: 100%;height: 150px;'></td>
</tr>我添加了'dataNieuwsbrieven‘,因为这一点很有效。下面是处理表单的函数:
function nieuwsbrieven() {
if (isset($this->data)) {
echo 'DATA:';
var_dump($this->data);
echo 'FILES:';
var_dump($_FILES);
//echo $_FILES['data[Nieuwsbrieven][attachment]']['name']."<< THE NAME";
exit();
}
else {
echo "data bestaat niet";
}
if(!empty($this->data)) {
$this->data['Nieuwsbrieven']['datum'] = mktime();
$datum = explode('-', $this->data['Nieuwsbrieven']['convert_datum']);
$this->data['Nieuwsbrieven']['plan_datum'] = mktime(0, 0, 0, $datum[1], $datum[0], $datum[2]);
$this->Nieuwsbrieven->save($this->data);
//$this->redirect("/systeem/verzenden/");
}
}如您所见,我var_dump $this->data和$_FILES,下面是该数据的输出:
DATA: array(1) { ["Nieuwsbrieven"]=> array(5) { ["content"]=> string(21) "
test content
" ["titel"]=> string(12) "test subject" ["convert_datum"]=> string(10) "25-09-2014" ["omschrijving"]=> string(4) "test" ["attachment"]=> string(16) "137785222989.jpg" } } FILES: array(0) { }INI设置没有问题,因为我用普通PHP尝试了一个测试脚本,并且能够很好地上传文件。
发布于 2014-09-23 08:16:15
试着用下列表格声明表格:
<form action='/systeem/nieuwsbrieven/' method='POST' enctype="multipart/form-data">CakePHP
echo $this->Form->create('User', array('type' => 'file'));发布于 2014-09-23 09:31:40
一个更好的选择是使用cakephp的表单助手,您应该用文件传递类型参数,而不是通常的post
echo $this->Form->create('User', array('type' => 'file'));请检查http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
发布于 2014-09-23 08:22:00
你想念那套..。
当表单包含任何元素时,使用表单标记添加多部分/表单数据。喜欢:<form action='/systeem/nieuwsbrieven/' method='POST' enctype="multipart/form-data">
https://stackoverflow.com/questions/25990047
复制相似问题