CakePHP Upload - Изменить имя файла после загрузки

1

Я использую плагин загрузки Jose Diaz-Gonzalez для загрузки файлов и изменения их размера.

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

Он отлично работает, за исключением того, что extension файла кажется отсутствующим в исходном файле. Я не уверен, как получить доступ к расширению файла, чтобы я мог добавить его к функции rename

Теги:
file-upload
cakephp

4 ответа

2
Лучший ответ

Я получаю решение.

В вашей модели:

'nameCallback' => 'filerename' <<< --- это нормально!

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

Получите правильное имя в своей базе данных. Это работает для меня.

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;
}
  • 0
    работать как шарм!
1

Я сделал это так, немного отличающийся от Данило Мигеля.

В $ actAs внутри массива имен полей я помещаю это:

'nameCallback'=>'renameFile',

И в моей функции:

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

И это сработало.

0

Попробуй это:

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

Я сделал это, и это сработало:

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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню