MeioUpload Behavior

By Vinicius Brandao Mendes (vbmendes)
A behavior to make the file uploads as simple as defining variables.
After about one year trying to make the upload simple in CakePHP, I finally finished an upload behavior based on digital spaghetti's (http://digitalspaghetti.tooum.net/) upload behavior (http://digitalspaghetti.tooum.net/switchboard/blog/2497:Upload_Behavior_for_CakePHP_12) and I am glad to show it for you.

You just have to set the MeioUpload Behavior:

Model Class:

Download code <?php 
class MyModel extends AppModel {
    var 
$actsAs = array('MeioUpload' => array(
        
'filename' => array(
            
'dir' => 'files/images',
        )
    );        
}
?>

And add the file input in the form:

View Template:

Download code
<?php echo $form->create('Produto',array('type' => 'file'));?>
<?php 
echo $form->input('filename', array('type' => 'file')); ?>

Here is the link to the snippet with the behavior code:
http://cakeforge.org/snippet/detail.php?type=snippet&id=226
And here id the link to the documentation:
http://www.meiocodigo.com/projects/meioupload

 

Comments 821

CakePHP Team Comments Author Comments
 

Comment

1 Great work! Thank you!

Great work! Thank you! But could you please translate error messages to English? Cos I don't understand some of them.
Posted Nov 8, 2008 by Stanislav Sokolov
 

Question

2 Is it possible to upload PDF and avi files?

Hi ,
Thanks for your Great Work!
I wonder if it is possible to upload PDF and video files by modifying MeioUpload Behavior.
Posted Nov 10, 2008 by stjamn
 

Comment

3 translations

Posted Dec 1, 2008 by Cosmin Cimpoi
 

Comment

4 Try Regular File Uploads First!

Save yourself 2 hours. Make sure a regular file upload works, before you try using this behavior.

I had some permissions issues and kept getting "Invalid File Extension" back as my error.

So ensure that you are able to upload a regular file first (through your controller or whatever), THEN drop in this bad boy.

Works like a charm. My only caveat is that it doesn't display validation errors with _added_ validation from another model.
Eg.

I use a Model (Images) with this behavior. Another Model (Product) "hasOne Image". I have custom validation on the "product_id" field for "isUnique" (I'm trying to enforce the hasOne image). If I upload straight through the Images model, everything is fine. If I upload through the Product Model the validation fails, but I don't get the error back.

A bit frustrating, but for all what this behavior does, it's worth it.
Posted Feb 2, 2009 by Baz L
 

Comment

5 wow

thanks for this great piece of code :)
Posted Apr 16, 2009 by Thomas
 

Comment

6 Translated error messages


'FieldName' => array(
    'rule' => array('uploadCheckFieldName'),
    'check' => true,
    'message' => 'This field was not defined in MeioUploadBehavior.'
),
'Dir' => array(
    'rule' => array('uploadCheckDir'),
    'check' => true,
    'message' => 'Cannot write to the directory.'
),
'Empty' => array(
    'rule' => array('uploadCheckEmpty'),
    'check' => true,
    'on' => 'create',
    'message' => 'The file cannot be empty.'
),
'UploadError' => array(
    'rule' => array('uploadCheckUploadError'),
    'check' => true,
    'message' => 'There was a problem uploading the file'
),
'MaxSize' => array(
    'rule' => array('uploadCheckMaxSize'),
    'check' => true,
    'message' => 'The file exceeds the maximum size.'
),
'InvalidMime' => array(
    'rule' => array('uploadCheckInvalidMime'),
    'check' => true,
    'message' => 'Invalid file type.'
),
'InvalidExt' => array(
    'rule' => array('uploadCheckInvalidExt'),
    'check' => true,
    'message' => 'Invalid file extension.'
)
Posted Apr 27, 2009 by Dimitry Zolotaryov
 

Question

7 Why gif isn't image? :)


...
            // It the file is an image, try to make the thumbnails
            if (count($options['allowed_ext']) > 0 && in_array($model->data[$model->name][$fieldName]['type'], array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
...

it's interesting, but gif is image too, and i need thumbnails for it too:)

It's writed that you need that thumbPhp for making thumbnails, but your behavior dont use it?.. you just have a function createthumb for making thumbs :)
Posted May 12, 2009 by Anton
 

Comment

8 Updated Behavior

I've fixed some things that were mentioned on both his blog and here. I've also managed to ACTUALLY use phpThumb. Please note that Nate's ThumbComponent is no longer necessary, and you can specify whether you would like to use ImageMagick or GD. There are some other extensions too, so take a look. Comments are welcome.

http://github.com/josegonzalez/MeioUpload/tree/master
Posted May 17, 2009 by Jose Diaz-Gonzalez
 

Comment

9 Update version

I've fixed some things that were mentioned on both his blog and here. I've also managed to ACTUALLY use phpThumb. Please note that Nate's ThumbComponent is no longer necessary, and you can specify whether you would like to use ImageMagick or GD. There are some other extensions too, so take a look. Comments are welcome.
Hi, where we can find the fixed version?
Thanks in advance.
Posted May 19, 2009 by Gabriel A Gonzalez
 

Comment

10 Forgot Link

I've fixed some things that were mentioned on both his blog and here. I've also managed to ACTUALLY use phpThumb. Please note that Nate's ThumbComponent is no longer necessary, and you can specify whether you would like to use ImageMagick or GD. There are some other extensions too, so take a look. Comments are welcome.
Hi, where we can find the fixed version?
Thanks in advance.

As is customary, I forgot the link. I've updated my comment and am also including a link here.

http://github.com/josegonzalez/MeioUpload/tree/master
Feel free to yell if anything breaks horribly :)
Posted Jul 6, 2009 by Jose Diaz-Gonzalez
 

Comment

11 Smooth

GODDAMM! Worked perfectly the first time.

Thank you all who worked. Congratulations.
Posted Aug 4, 2009 by Lucas Costa
 

Comment

12 error messages are not shown

Hi, i tried the behavior from git but error messages are not shown? Any idea?

Thanks
Posted Aug 31, 2009 by Daniel
 

Comment

13 multiple upload

this is the best upload image behavior and i use it several times, how to make it multiple upload? thanx :)
Posted Oct 4, 2009 by Benny