Oct 12 2008

MeioUpload Behavior 1.0.1 released!

Category: CakePHP,PHPvbmendes @ 19:40

I am glad to show you the MeioUpload Behavior 1.0.1. An improved Upload Behavior for CakePHP 1.2. It makes file uploads as simple as defining a variable.

See the docs.

Features

  • Accepts custom directory for files to be uploaded;
  • Validates the file extension and mime-type due to the behavior configuration;
  • Validates the max file size;
  • Allow custom validation rules;
  • Allow as many thumbnails formats as you want;
  • Allow more then one field to be uploadable, with custom options per field;
  • Stores the directory, filesize, and mime-type in the database if the table has these fields;
  • Allow the use of default files and deleting files without deleting the entire record;
  • Delete files when the record is deleted or updated with a new file;
  • Also works in the $model->saveAll method.

Changelog

v1.0.1

  • Fixed a bug in the create folder method;
  • Now you can use the $validate var of the model to apply the changes to default validation rules;
  • Changed the my_array_merge function, now it’s part of the behavior, name arrayMerge;
  • Allow use of {DS}, {model} and {field} constants in directory name and fields names;
  • Fixed a bug with the replacement of the default names.

v1.0

  • Initial release

Any bugs, features suggestions, and english errors, please tell me.

Tags: , ,

11 Responses to “MeioUpload Behavior 1.0.1 released!”

  1. matheus says:

    esse site tem códigos ótimos! tornou minha vida muita mais fácil!

    parabéns!

  2. vbmendes says:

    Obrigado pelo reconhecimento. Qualquer dúvida é só falar.

  3. ADmad says:

    Nice work. You have done a fine job improving upon the work of the previous authors. A few comments: - Didn’t core’s Set::merge work for you instead of you custom recursive merge function ? - Not that it matters much but I think a more appropriate location to merge validation rules with the model’s $validate would be beforeValidate callback. - You should submit this fine piece of work to the bakery. Its deserves more exposure :)

  4. Vinicius Mendes says:

    Thank’s for the comment, I will take note of these suggestions and analyze the possibility of implementing them. I saw the Set::merge function a little while after publishing the behavior but already changed the code to use it, I will publish it soon.

    About the validation, I’m thinking about changing some things in it. Maybe use the var $validate of the model to apply the validation rules. I think it is more related to the work the framework is doing.

    And about the bakery, I’m trying to have time to do this. Anyway, thanks for the comment again.

  5. ADmad says:

    Comments round 2 :)

    • In beforeSave when attempting to move uploaded file don’t set the error message directly to validationErrors array . Instead use the Model->invalidate function. Many people override this function for il8n and translate the message string first before setting to validationErrors.

    • Not important but declaring the default validation errors twice as $default_options['validations'] and as $default_validations seems a bit redundant. You can just have $default_options['validations'] and assign it to $default_validations in setup() if needed.

  6. Vinicius Mendes says:

    Hehe

    thanks for the help. Fell free to make as many rouds as you want. I will see this invalidate function. And about the arrays, I will keep just the $default_validations, Since it merges both. I don’t removed the $default_options['validations'] just to keep the backwards compatibility.

  7. kanoa says:

    Nice work, I was suffering from corrupted uploads on MAMP running OSX10.5.3. Inserting this block of code at line 636 to set the permissions of the uploaded file fixed the problem:

            // change the permissions of the uploaded file
            if(!chmod( $saveAs, 0644 ) ) {
                $model->validationErrors[$field] = 'Correct permissions could not be set on the uploaded file';
                return false;
            }
    
  8. Vinicius Mendes says:

    Kanoa, thanks for the contribution, I will add this piece of code in the behavior.

  9. ADmad says:

    Setting fixed file permissions is not a good idea. Keep it configurable so that permissions can be set as required based on server needs.

  10. Vinicius Mendes says:

    Hmm, good idea. I will do this. An option, if empty, don’t set permissions, else set the permission based in this string. The default will be it empty.

  11. Francis Rebouças says:

    Olá Vinicius…. Muito bom o seu behavior, sou um novo aventureiro do CakePHP, meu problema é o seguinte tenho que enviar uma lista de imagens de uma só vez pelo seu Behavior, por isso optei por um flash que cria a lista e as envia. As informações do arquivo fica em um $_FILES['Filedata'].

    Meu Código: if (!empty($_FILES["Filedata"])) { $this->Imagem->create(); $this->data['Imagem']['imagem']['name'] = $_FILES['Filedata']['name']; $this->data['Imagem']['imagem']['type'] = $_FILES['Filedata']['type']; $this->data['Imagem']['imagem']['tmp_name'] = $_FILES['Filedata']['tmp_name']; $this->data['Imagem']['imagem']['error'] = $_FILES['Filedata']['error']; $this->data['Imagem']['imagem']['size'] = $_FILES['Filedata']['size']; $this->Imagem->save($this->data); }

    Mas ele não resulta em nada. Não guarda nada em DB nem faz upload.

    Obrigado