开发者

how to upload the File to some location in cakephp

开发者 https://www.devze.com 2022-12-09 12:23 出处:网络
i am trying to use the File upload feature in my application .. i am having a Form with many fields of type Text,textarea and including File upload FIeld.

i am trying to use the File upload feature in my application ..

i am having a Form with many fields of type Text,textarea and including File upload FIeld.

I have kept a Submit button at the end of the Form which on click will submit the actual value of the textbox /textarea and even the value for the Field of type File upload.

How to get the actual file that is uploaded and to save it in a location so that i can view the uploaded file later on ..

The code that i have used is, Edit :

i have added enctype in the Form tag but not working while submission

  <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>

Action Submit in the Cakephp controller is

   function submit($fo开发者_StackOverflow社区rmid = 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 .');
        }

    }


In /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;
}

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

In 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消