How to do form-based file uploads in CakePHP?

14,905

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.

Share:
14,905
Russell
Author by

Russell

Updated on July 29, 2022

Comments

  • Russell
    Russell almost 2 years

    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 got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!