在PHP中将上传的文件保存到目录之前,如何重命名该文件?
我在PHP项目中使用这个简单的上传和调整图像大小来完成我的项目。 但我不知道上传后如何重命名文件。 我想重命名文件并将其上传到服务器和数据库中。代码如下:在PHP中将上传的文件保存到目录之前,如何重命名该文件?,php,Php,我在PHP项目中使用这个简单的上传和调整图像大小来完成我的项目。 但我不知道上传后如何重命名文件。 我想重命名文件并将其上传到服务器和数据库中。代码如下: public function run() { $total = count($this->files); for($i=0; $i<$total; $i++) { if (empty($this->files['name'][$i]) === false) { if
public function run() {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
if (empty($this->files['name'][$i]) === false) {
if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$this->files['name'][$i]) == false) {
$this->errors['type'] = "run";
$this->errors['file_name'] = $this->files['name'][$i];
}
}
}
return empty($this->errors);
}
公共函数运行(){
$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);
}
我使用了很多方法,例如basename
不可能,你知道吗??感谢您不要使用
移动上传的文件
按照下面给出的代码示例使用它
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
你可以这样做
public function run() {
$total = count($this->files);
for($i=0; $i<$total; $i++) {
if (empty($this->files['name'][$i]) === false) {
$tempName = explode(".", $this->files['name'][$i]);
$newName = "IMAGE_NAME"; //generate unique string to avoid conflict
$newfilename = $newName . '.' . end($tempName );
if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$newfilename) == false) {
$this->errors['type'] = "run";
$this->errors['file_name'] = $this->files['name'][$i];
}
}
}
return empty($this->errors);
}
公共函数运行(){
$total=count($this->files);
对于($i=0;$ifiles['name'][$i])==false){
$tempName=explode(“.”,$this->files['name'][$i]);
$newName=“IMAGE\u NAME”;//生成唯一字符串以避免冲突
$newfilename=$newName.'..end($tempName);
如果(移动上传的文件($this->files['tmp\u name'][$i],$this->store\u directory.'/'.$newfilename)==false){
$this->errors['type']=“run”;
$this->errors['file_name']=$this->files['name'][$i];
}
}
}
返回空($this->errors);
}
在$newName
中,可以为文件生成新名称
更新
为避免冲突,请在每次生成名称时使用唯一字符串。这是您的解决方案
public function run(){
$total = count($this->files);
for($i=0; $i<$total; $i++){
if(empty($this->files['name'][$i]) === false){
$ext = substr(strtolower(strrchr($this->files['name'][$i], '.')), 1); //get the extension
$new_name = 'new-name'.date('dmY').$ext;
if (move_uploaded_file($this->files['tmp_name'][$i], $this->store_directory.'/'.$new_name) == false) {
$this->errors['type'] = "run";
$this->errors['file_name'] = $this->files['name'][$i];
}
}
}
return empty($this->errors);
}
公共函数运行(){
$total=count($this->files);
对于($i=0;$ifiles['name'][$i])==false){
$ext=substr(strtolower(strrchr($this->files['name'][$i],'.')),1);//获取扩展名
$new_name='new name'。日期('dmY')。$ext;
如果(移动上传的文件($this->files['tmp\u name'][$i],$this->store\u directory.'/'.$new\u name)==false){
$this->errors['type']=“run”;
$this->errors['file_name']=$this->files['name'][$i];
}
}
}
返回空($this->errors);
}
可能重复的请接受任何一个答案…如果我们按照您的建议使用像IMAGE\u NAME
这样的固定字符串,则可能会出现这样的情况:如果再次上载相同的文件,则服务器上具有此名称的图像将被替换。因此,最好使用一些名称约定,这将有助于避免这种情况。我在回答中已经考虑到了这一点。