how to enable cakephp media plugin to upload pdf files and docs - cakephp

I apply the media plugin in my project but this is only for jpeg and png..
you can see in the demo...
demo
There is a validation that allow pdf files but doesnt work.
in attachment.php
var $validate = array(
'file' => array(
'resource' => array('rule' => 'checkResource'),
'access' => array('rule' => 'checkAccess'),
'location' => array('rule' => array('checkLocation', array(
MEDIA_TRANSFER, '/tmp/'
))),
'permission' => array('rule' => array('checkPermission', '*')),
'size' => array('rule' => array('checkSize', '5M')),
'pixels' => array('rule' => array('checkPixels', '1600x1600')),
'extension' => array('rule' => array('checkExtension', false,
array(
'jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif', 'pdf', 'tmp'
))),
'mimeType' => array('rule' => array('checkMimeType', false, array(
'image/jpeg', 'image/png', 'image/tiff', 'image/gif',
'application/pdf'
)))),
'alternative' => array(
'rule' => 'checkRepresent',
'on' => 'create',
'required' => false,
'allowEmpty' => true,
));
this is the default configuration but doesnt work for pdf files only for images.

If you are using CakePHP 2.x, I would recommend the AjaxMultiUpload plugin which should take care of this for you:
http://bakery.cakephp.org/articles/srs2012/2012/03/12/ajaxmultiupload_plugin_for_cake_2_0_x_and_2_1

Related

Cannot validate mimetype for a missing file

<?php
public function validationDefault(Validator $validator)
{
$validator->add('image', [
'uploadError' => [
'rule' => 'uploadError',
'message' => __d('Message', 'The logo upload failed.'),
'last' => true
],
'mimeType' => [
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => __d('Message', 'Please upload images only (gif, png, jpg).')
],
'fileSize' => [
'rule' => array('fileSize', '<=', '1MB'),
'message' => __d('Message', 'Logo image must be less than 1MB.')
],
])
->allowEmpty('image');
The above validation code got the error..
$validator
->requirePresence('owner', 'create')
->notEmpty('owner');
return $validator;
}
}
?>
Whats wrong with my validation code? when i comment the validation in image field it will save into database.,
But when i dont comment, it will always said "Cannot validate mimetype for a missing file"
Try this one
'image' => array(
'uploadError' => array(
'rule' => 'uploadError',
'message' => __d('Message', 'The logo upload failed.'),
'last' => true,
'allowEmpty' => true
),
'mimeType' => (
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => __d('Message', 'Please upload images only (gif, png, jpg).'),
'allowEmpty' => true
),
'fileSize' => (
'rule' => array('fileSize', '<=', '1MB'),
'message' => __d('Message', 'Logo image must be less than 1MB.'),
'allowEmpty' => true
),
)
You can use also use extension rule for validating file type like below:
'image' =>array(
'extension' => array(
'rule' => array('extension',array('gif', 'png', 'jpg', 'jpeg'),
'message' => __d('Message', 'Please upload images only (gif, png, jpg).')
)
),
MY guess is one of the rules is throwing an exception (likely to be mimeType) because of error in upload process.many php level configurations control $_FILES array population, one such setting is upload_max_filesize. As a result any file function which depend on file availablity will not work properly.
whats wrong with my validation code ?
Nothing. This is the intended behavior, all validation rules are being run no matter if a previous one failed. So in your case, an upload error will be recognized, but validation will still proceed to the next rule, which is then going to fail hard as no file has been uploaded.
This behavior can be disabled by using the last option. Marking a rule as last will cause validation for that field to stop in case the rule fails.
'uploadError' => [
'rule' => 'uploadError',
'message' => __d('clients', 'The logo upload failed.'),
'last' => true
],

cakephp 2.4 upload an image

i'm trying to upload an image and make a thumbnail of this image by using this Behavior (github) tutorial
I'm using the blog tutorial on cakephp website.
I'm getting this following errors each time I want to upload the images. I'm dowloading an .jpeg file
Invalid file type. Only .jpg, .jpeg, .png, .gif allowed.
My Model/Post.php
<?php
class Post extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
public $actsAs = array('ImageUpload' => array(
'image' => array(
'required' => false,
'directory' => 'img/uploads/',
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
'allowed_extension' => array('.jpg', '.jpeg', '.png', '.gif'),
'allowed_size' => 2097152,
'random_filename' => true,
'resize' => array(
'thumb' => array(
'directory' => 'img/uploads/thumbs/',
'phpThumb' => array(
'far' => 1,
'bg' => 'FFFFFF',
'zc' => 0
),
'width' => 230,
'height' => 150
),
'max' => array(
'directory' => 'img/uploads/thumbs/',
'phpThumb' => array(
//'far' => 1,
//'bg' => 'FFFFFF',
'zc' => 0
),
'width' => 400,
'height' => 300
)
)
)
)
);
}

Cakephp image -Can not determine the mimetype

cakephp 2.3
I'm uploading an image and I have an error saying that:
Can not determine the mimetype.
Error: An Internal Error Has Occurred.
On my Model this is a part of my $validation
'file_name' => array(
'uploadError' => array(
'rule' =>'uploadError',
'message' => 'Your image upload failed',
'allowEmpty' => FALSE,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'mimeType' => array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => FALSE,
),
'fileSize' => array(
'rule' => array('fileSize', '<=', '2MB'),
'message' => 'Your image must be less than 2MB or(2048ko).',
'allowEmpty' => FALSE,
),
'processCoverUpload' => array(
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => FALSE,
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This file name is already exist in your folder',
'required' => 'create',
'allowEmpty' => FALSE,
),
),
I'm only allow 3 types of mimetype. any help?
I just ran into exactly the same problem. Thanx to some other comments which pointed me in the right direction, here is my solution:
Edit php.ini (\xampp\php\php.ini on Win7) search for extension=php_fileinfo.dll and uncomment it.
FYI: I'm running xampp 1.7.7 [Apache:2.2.21; PHP:5.3.8; MySQL:5.5.16]. Hopefully on newer xampp versions the extension is enabled by default.

Can't upload image using Miles Johnson Uploader

I'm developing an application, where in I've created a products table that has following attributes.
{id, image, warranty-image, created, modified}
I'm using miles johnson 4.3.1 uploader for uploading images. So I've written $actsAs in Product model as follows.
public $actsAs = array(
'Uploader.Attachment' => array(
'image' => array(
'overwrite' => true,
'uploadDir' => 'img/products',
'finalPath' => '',
'dbColumn' => 'image',
'transforms' => array(
'imageLarge' => array(
'nameCallback' => 'transformNameCallback',
'method' => 'resize',
'prepend' => 'large_',
'width' => 750,
'height' => 100,
'aspect' => false
)
),
'warranty_image' => array(
'overwrite' => true,
'uploadDir' => 'img/products',
'finalPath' => '',
'dbColumn' => 'warranty_image',
'transforms' => array(
'warranty_imageSmall' => array(
'nameCallback' => 'transformNameCallback',
'method' => 'resize',
'prepend' => 'small_',
'width' => 150,
'height' => 96,
'aspect' => false
)
)
)
),
'Uploader.FileValidation' => array(
'image' => array(
'required' => true,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'type' => array('image'),
'minWidth' => 100,
'minHeight' => 100,
'filesize' => 5242880
),
'warranty_image' => array(
'required' => true,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'type' => 'image',
'minWidth' => 100,
'minHeight' => 96,
'filesize' => 5242880
)
)
);
public function transformNameCallback($name, $file) {
return $this->getUploadedFile()->name();
}
In add view I've written file inputs as follows.
echo $this->Form->create('Product', array('enctype' => 'multipart/form-data'));
echo $this->Form->file('image');
echo $this->Form->file('warranty_image');
echo $this->Form->end();
This uploader just uploading only image image but not warranty_image image. Please help me to get the solution. The work would be more appreciated.
I would recommend https://github.com/josegonzalez/cakephp-upload
This works very similarly to how you are looking and actually does work correctly.
Use HTML input tag in view or CakePHP Form Helper Input tag.
echo $this->Form->input('image', array('type' => 'file', 'required' => false, 'label' => 'Select the File to Upload'));
echo $this->Form->input('warranty_image', array('type' => 'file', 'required' => false, 'label' => 'Select the File to Upload'));
Your database table is using "warranty-image" as the field.
But your PHP code is using "warranty_image".
Note the "-" instead of "_"

Meio upload behavior - zoom crop not working

I'm using this MeioUpload Behavior. And i installed both phpThumb component and phpThumb vendor.
Thumbnail generation is working fine but zoom-cropping is not.
Here is my code. You can see i've tried every possible variables. I also set every default zc or zoom_crop variables I found in component and vendor to 1. 1 is the same as C anyway.
var $actsAs = array('MeioUpload' => array( 'filename' => array(
'dir' => "uploads/images",
'create_directory' => true,
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'),
'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'),
'thumbsizes' => array(
'small' => array('width'=>65, 'height'=>60,'zc'=>"C", 'zoomCrop' => "C", 'zoom_crop'=>"C"),
'medium' => array('width'=>159, 'height'=>130, 'zc'=>"C", 'zoomCrop' => "C", 'zoom_crop'=>"C"),
'large' => array('width'=>480, 'height'=>320,'zc'=>"C", 'zoomCrop' => "C", 'zoom_crop'=>"C")
)
)
)
);
Any idea?
in latest version is necessary to define thumbnail precisely. I'm not sure if this solve your problem with script in older version.
var $actsAs = array(
'MeioUpload' => array(
'img_file' => array(
'create_directory' => true,
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
'allowed_ext' => array('.jpg', '.jpeg', '.png'),
'zoomCrop' => true,
'thumbsizes' => array(
'normal' => array('width' => 400, 'height' => 300),
'small' => array('width' => 80, 'height' => 80,'maxDimension' => '', 'thumbnailQuality' => 100, 'zoomCrop' => true),
),
'default' => 'default.jpg'
)
));
Ok, I gave up the use of MeioUpload to generate-and-save thumbnails.
Instead, I generate thumbnails on-the-fly, directly using phpThumb.
Like this
<img src="<?=$this->webroot?>phpThumb/phpThumb.php?src=../uploads/images/<?=$event['Image'][0]['filename']?>&w=159&h=130&zc=1" alt="Event Picture">
It's a little sacrifice for performance but every easy and flexible.

Resources