Wednesday, June 18, 2008

File Uploads with FUSE PHP MVC Framework

I had a project today that required that PDF files be attached to job orders, and FUSE made the PDF upload a simple task. All I had to do was add an after_add() method in my controller to handle the upload, as shown below. The filename is the id of the job that was just added to the database, with a PDF extension:


function after_add() {

if ( $this->is_postback() ) {
FUSE::Require_class('File/FileUpload');

$upload = new FileUpload();
$upload->file_input_name = 'waiver_file';
$upload->destination_dir_create = true;
$upload->allow_file_extension( 'pdf' );

$upload->destination_path = APP_BASE_PATH . DIRECTORY_SEPARATOR . 'files'
. DIRECTORY_SEPARATOR . $this->job->id . ".pdf";

$upload->process();

parent::after_add();
}
}


Note that I call parent::after_add() once I'm finished so that the regular FuseApplicationController after_add() method can take over and display a "changes saved" message to the user.