file upload in cakephp 2.3

前端 未结 4 1668
庸人自扰
庸人自扰 2020-12-18 01:11

I\'m new in cakephp and i\'m trying to create a simple file upload with cakephp 2.3 here is my controller

public function add() {
    if ($this->request-&         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 01:24

    I've found the complete guide to uploading files and images in CakePHP from here - Handling File Uploads in CakePHP

    The example code is given below.

    Controller:

    $fileName = $this->request->data['file']['name'];
    $uploadPath = 'uploads/files/';
    $uploadFile = $uploadPath.$fileName;
    if(move_uploaded_file($this->request->data['file']['tmp_name'],$uploadFile)){
        //DB query goes here
    }
    

    View:

    Form->create($uploadData, ['type' => 'file']); ?>
        Form->input('file', ['type' => 'file', 'class' => 'form-control']); ?>
        Form->button(__('Upload File'), ['type'=>'submit', 'class' => 'form-controlbtn btn-default']); ?>
    Form->end(); ?>
    

提交回复
热议问题