How to do form-based file uploads in CakePHP?

前端 未结 3 1189
眼角桃花
眼角桃花 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:32

    NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.

    In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.

    You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

    0 讨论(0)
  • 2021-02-06 18:33

    You can use Zend Components to handle the file upload. There is a good example here on my website: CakePHP file upload using Zend Components

    0 讨论(0)
  • 2021-02-06 18:44

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

    <?php echo $form->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.

    0 讨论(0)
提交回复
热议问题