1

I am using Jose Diaz-Gonzalez's Upload Plugin for uploading files and resizing them.

public $actsAs = array(
      'Containable',
    'Upload.Upload' => array(
        'filename' => array(
            'fields' => array(
                'dir' => 'gallery'
            ),
                        'thumbnailSizes' => array(
                                'small' => '500x500',
                        ),
                        'thumbnailMethod' => 'php',
                        'path' => '{ROOT}webroot{DS}img{DS}{model}{DS}',
                        'nameCallback' => 'filerename'
        )
    )
  );

function filerename($currentName) {
        debug($data);
        debug($currentName);
        return uniqid();
    }

It works fine except that the file extension seems to be missing on the original file. I am not sure how to access the extension of the file so that I can append the same to the rename function

4 Answers 4

2

I get the solution.

In your model:

'nameCallback' => 'filerename' <<<--- this is OK!

public function filerename()
{
    return date("Y_m_d H_i");
}

Get the correct name in your database. This works for me.

public function beforeSave($options = array()) 
{
    // Cambiar el nombre a la foto de perfil
    if( isset($this->data['User']['photo']) )
    {
        $exp = array();
        $exp = explode("/", $this->data['User']['type']);
        $this->data['User']['photo'] = date("Y_m_d H_i") .".". $exp[1];
    }

    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I did it this way, a little bit different from that by Danilo Miguel.

In $actsAs inside the field's name array, I put this:

'nameCallback'=>'renameFile',

And in my function:

public function renameFile($currentName,$data,$field){
    //current name = name of file
     $name = explode('.',$data); //name to array --convierte el nombre a array
     $index=sizeof($name)-1;//get the index of the extension -- obtiene la posicion de la extencion
     $extencion=$name[$index]; //extension -- obtiene la extencion
     $sluged=Inflector::slug($field['Anuncio']['titulo'],$replacement='-'); //uses the method slug to get new name using another field of the request data -- uso el nombre de otro campo de los datos al hacer el post
     $created=date("Y-m-d-h-i-sa");
     //return the new name -- retorna el nuevo nombre :V
     return $sluged.'-'.$created.'.'.$extencion;
}

And it worked.

Comments

0

I did this way, and it worked:

public function filerename($currentName,$data,$field) {
    $part = explode('/',$field['User']['type']);
    return md5(uniqid(rand(),true)).'.'.$part[1];
}  

Comments

0

Try this :

function renameFile($currentName,$data,$field) {
$array = explode(".",$data);
$newName = md5(time().rand(1111,99999999)).".".end($array);
 return $newName;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.