How to do form-based file uploads in CakePHP?

前端 未结 3 1190
眼角桃花
眼角桃花 2021-02-06 18:04

I have been looking into this for a while and can\'t figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have go

3条回答
  •  你的背包
    2021-02-06 18:44

    Firstly your form needs to be set up to allow file uploads.

    create(Model, array('type' => 'file')); ?>
    

    This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).

    Once the file has been uploaded you should handle everything else from within the Model:

    function beforeSave($created) {
        extract($this->data[Model][field]);
        if ($size && !$error) {
            move_uploaded_file($tmp_name, destination);
            $this->data[Model][field] = destination;
        }
        return true;
    }
    

    These are only the basics, so be sure to have a play around to find the solution that best fits your needs.

提交回复
热议问题