【问题标题】:how to upload the File to some location in cakephp如何将文件上传到 cakephp 中的某个位置
【发布时间】:2009-10-20 06:07:33
【问题描述】:

我正在尝试在我的应用程序中使用文件上传功能..

我有一个表单,其中包含许多 Text、textarea 类型的字段,包括文件上传字段。

我在表单末尾保留了一个提交按钮,单击该按钮将提交文本框 /textarea 的实际值,甚至是文件上传类型的字段的值。

如何获取上传的实际文件并将其保存在某个位置,以便我以后可以查看上传的文件..

我使用的代码是, 编辑:

我在表单标签中添加了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 控制器中的 Action Submit 是

   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 .');
        }

    }

【问题讨论】:

  • 嗨,如果我在 Form 标签中添加 enctype 它不起作用
  • 你开始使用 Cake 怎么样?尝试完全按照手册中的说明进行操作。 (提示:您的具体问题是文件字段的名称,但更大的问题是您没有使用 Cake。)
  • 即使我的字段为 如何从数据中获取 Untitled3 [Form][Untitled3] 以便使用它通过 $this->data['Form']['Untitled3']['tmp_name']; 获取 tmp_name;

标签: cakephp


【解决方案1】:

在/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();

【讨论】:

  • 控制器部分怎么知道 data['Upload']['File'] 是字段的名称,因为在我的情况下,我的表单可能包含许多字段,包括文件上传字段..所以我怎么知道特定字段的名称为 data['Upload']['File']
  • 表单助手将 Upload.File 转换为 data[Upload][File]。 html 输出如下所示: data);到您的控制器操作以查看发布的内容。在我的示例中,Upload 是模型的名称,File 是字段名称。
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2017-09-29
  • 2014-01-24
  • 2017-03-30
  • 2012-01-01
  • 2010-11-18
  • 1970-01-01
相关资源
最近更新 更多