CakePHP上传-上传后更改文件名
我正在使用上载文件并调整其大小CakePHP上传-上传后更改文件名,php,cakephp,file-upload,Php,Cakephp,File Upload,我正在使用上载文件并调整其大小 public $actsAs = array( 'Containable', 'Upload.Upload' => array( 'filename' => array( 'fields' => array( 'dir' => 'gallery' ), 'thumbnailSiz
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();
}
它工作正常,只是原始文件中似乎缺少文件扩展名。我不知道如何访问文件的扩展名,以便将其附加到重命名
函数中我得到了解决方案
在您的模型中:
'nameCallback'=>'filerename'我是这样做的,它起了作用:
public function filerename($currentName,$data,$field) {
$part = explode('/',$field['User']['type']);
return md5(uniqid(rand(),true)).'.'.$part[1];
}
我是这样做的,和丹尼洛·米格尔的有点不同
在字段名称数组中的$actsAs中,我放置了以下内容:
'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;
}
它成功了。试试这个:
function renameFile($currentName,$data,$field) {
$array = explode(".",$data);
$newName = md5(time().rand(1111,99999999)).".".end($array);
return $newName;
}
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;
}
function renameFile($currentName,$data,$field) {
$array = explode(".",$data);
$newName = md5(time().rand(1111,99999999)).".".end($array);
return $newName;
}