上载图像PHP重命名文件

上载图像PHP重命名文件,php,image-processing,Php,Image Processing,当谈到PHP时,我是一个非常初级的人,但是,我似乎有我的图像上传程序以我想要的方式使用下面的代码(来源于在线)。唯一的问题是,当用户将文件上传到像user/date这样的唯一文件时,我需要重命名这些文件。当我修改这个代码时,我似乎无法让它工作 我花了一些时间在这里和互联网上搜索,但我很难找到合适的东西 请有人给我一个解决办法,或者给我指明正确的方向 谢谢你,堆栈溢出 类上传器{ private$files=array(); private$extensions=array(); private

当谈到PHP时,我是一个非常初级的人,但是,我似乎有我的图像上传程序以我想要的方式使用下面的代码(来源于在线)。唯一的问题是,当用户将文件上传到像user/date这样的唯一文件时,我需要重命名这些文件。当我修改这个代码时,我似乎无法让它工作

我花了一些时间在这里和互联网上搜索,但我很难找到合适的东西

请有人给我一个解决办法,或者给我指明正确的方向

谢谢你,堆栈溢出

类上传器{
private$files=array();
private$extensions=array();
private$errors=array();
私有$store_目录=“./attachment”;
private$resize\u image\u library\u instance=null;
公共函数构造($files){
if(is_数组($files)==false){
$files[]=$files;
}
$this->files=$files;
}
公用函数集\u上传到($store\u目录){
$this->store\u directory=$store\u directory;
}
公共函数集\u有效\u扩展($extensions,$case\u sensitive=false){
$this->extensions=$extensions;
$this->case\u sensitive=$case\u sensitive;
}
公共函数集\调整大小\图像\库($resize\图像\库\实例){
$this->resize\u image\u library\u instance=$resize\u image\u library\u instance;
}
公共函数是有效的扩展(){
$total=count($this->files);
对于($i=0;$ifiles['name'][$i])==false){
$file_extension=$this->get_extension($this->files['name'][$i]);
if(在数组中($file\u扩展名,$this->extensions)==false){
$this->errors['type']=“extension”;
$this->errors['file_name']=$this->files['name'][$i];
$this->errors['file\u extension']=$file\u extension;
返回false;
}
}
}
返回true;
}
公共功能运行(){
$total=count($this->files);
对于($i=0;$ifiles['name'][$i])==false){
如果(移动上传的文件($this->files['tmp\u name'][$i],$this->store\u directory.'/'。$this->files['name'][$i])==false){
$this->errors['type']=“run”;
$this->errors['file_name']=$this->files['name'][$i];
}
}
}
返回空($this->errors);
}
公共功能调整大小($scale\u size=200){
$total=count($this->files);
对于($i=0;$istore_directory.'/'.$this->files['name'][$i]);
如果(file_exists($image)==true&&is_文件($image)==true){
$this->resize\u image\u library\u instance->init($image);
$this->resize\u image\u library\u instance->scale($scale\u size);
如果($this->resize\u image\u library\u instance->save($image)==false){
$this->errors['type']=“resize”;
$this->errors['file\u name']=$image;
}
}
}
返回空($this->errors);
}
公共函数get_errors(){
返回$this->errors;
}
//
私有函数get_扩展名($filename){
$info=pathinfo($filename);
返回$info['extension'];
}
私有函数get_filename($file){
$info=pathinfo($file);
返回$info['filename'];
}

}
要为上载的文件生成唯一名称并将其保存到服务器,您可以使用time()、mt_rand()和上载的文件名。像这样

public static function generateUniqueFileName($fileName)
{
    return time() . mt_rand(100, 100000) . $fileName;
}
如果您想预先挂起用户名,那么可以将其传递给方法,并在开始时对其进行处理