MeioUpload Behavior
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:
And add the file input in the form:
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
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
Comment
1 Great work! Thank you!
Question
2 Is it possible to upload PDF and avi files?
Thanks for your Great Work!
I wonder if it is possible to upload PDF and video files by modifying MeioUpload Behavior.
Comment
3 translations
Comment
4 Try Regular File Uploads First!
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.
Comment
5 wow
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.'
)
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 :)
Comment
8 Updated Behavior
http://github.com/josegonzalez/MeioUpload/tree/master
Comment
9 Update version
Hi, where we can find the fixed version?
Thanks in advance.
Comment
10 Forgot Link
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 :)
Comment
11 Smooth
Thank you all who worked. Congratulations.
Comment
12 error messages are not shown
Thanks
Comment
13 multiple upload