Maths RkBala Maths RkBala - 2 months ago 16
PHP Question

Upload image name issue when share the social sites Upload plugin in Cakephp 2.x

I am told about Image upload plugin version for cakephp2.x not the cakephp3.x

I am using Upload plugin for image upload in cakephp2.x. It is a nice plugin.

https://github.com/szajbus/uploadpack

Problem:

If upload image name like

Koala - animal.jpg
plugin stored the name as
<id>_Koala - animal.jpg

When we share the image to social site.

Image is not sharing the social site due to (image name)space issue.

So I want store the image name like
<id>_Koala_-_animal.jpg

Answer

I found the solutions after one and half days:

Add the following code in the following file

Upload\Model\Behavior\UploadBehavior.php

public function beforeSave(Model $model,$options=array()) {

    //Existing code

    /*Fixes code before "return true;" start*/
    if(isset($model->data[$model->alias][$field])){
        $model->data[$model->alias][$field] = str_replace(' ','_',$model->data[$model->alias][$field]);
    }
    /*Fixes code before "return true;" End*/

    return true;
}