我正在尝试在我的应用程序中使用文件上传功能..
我的表单有很多字段,包括Text,textarea,包括File upload FIeld。
我在表单末尾保留了一个“提交”按钮,单击该按钮将提交文本框/文本区域的实际值,甚至是文件上传类型字段的值。
如何获取上传的实际文件并将其保存在某个位置,以便我以后可以查看上传的文件。
我使用的代码是, 编辑:
我在Form标签中添加了enctype但在提交时没有工作
<form method="post" action="/FormBuilder/index.php/forms/submit/93/13" id="ResultSubmit" enctype="multipart/form-data">
<div class="input file">
<label for="276">Choose Ur File To Upload</label>
<input type="file" value="" style="width: 400px;" id="276" name="Choose ur file to upload"/>
</div><br/>
<div class="input text">
<label for="277">Name</label>
<input type="text" value="" style="width: 200px;" id="277" name="Name"/>
</div> <br/>
<div class="submit"><input type="submit" value="submit"/></div>
</form>
Cakephp控制器中的操作提交是
function submit($formid = null,$fillerid=null)
{
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['submitter']=$this->Session->read('filler');
echo "submitter: ".$this->Session->read('filler');
$results=$this->Form->hasResults($this->data);
echo http_build_query($_POST);
if(empty($results)){
foreach ($_POST as $key => $value):
if(is_array($value)){
$value = implode('', $_POST[$key]);
$this->data['Result']['value']=$value;
}
else{
$this->data['Result']['value']=$value;
}
$this->data['Result']['form_id']=$formid;
$this->data['Result']['submitter_id']=$fillerid;
$this->data['Result']['label']=Inflector::humanize($key);
$this->data['Result']['submitter']=$this->Session->read('filler');
$this->Form->submitForm($this->data);
endforeach;
$this->Session->setFlash('Your entry has been submitted.');
$this->Invite->updateAll(array('Invite.filled'=>"'Yes'"),array('Invite.id'=>"$fillerid"));
}else{
$this->Session->setFlash('You have already filled the Form .');
}
}
答案 0 :(得分:4)
在/app/models/upload.ctp中:
function beforeSave()
{
if (!empty($this->data['Upload']['File']) && is_uploaded_file($this->data['Upload']['File']['tmp_name']))
{
if (!move_uploaded_file($this->data['Upload']['File']['tmp_name'], 'some_location/' . $this->data['Upload']['File']['name']))
{
return false;
}
$this->data['Upload']['name'] = $this->data['Upload']['File']['name'];
$this->data['Upload']['type'] = $this->data['Upload']['File']['type'];
$this->data['Upload']['size'] = $this->data['Upload']['File']['size'];
}
return true;
}
在/app/controllers/uploads_controller.php中:
function add()
{
if (!empty($this->data))
{
if ($this->Upload->save($this->data))
{
$this->Session->setFlash('File upload successful.');
$this->redirect('/uploads');
}
}
}
在app / views / uploads / add.ctp中:
echo $form->create('Upload');
echo $form->input('Upload.File', array('type' => 'file'));
echo $form->submit('Upload the file');
echo $form->end();