简体   繁体   中英

CakePHP Upload - Change file name once uploaded

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

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;
}

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.

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];
}  

Try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM