简体   繁体   English

如何在php中重命名文件?

[英]How to rename a file in php?

I am trying to rename the file if existing, the file will be moved successfully but the new file name will be like 0. the file will be shown only if I add .png manually to the name. 我正在尝试重命名该文件(如果存在),该文件将被成功移动,但是新文件名将类似于0。仅当我手动将.png添加到名称中时,该文件才会显示。 I am not able to let the php rename the file correctly. 我无法让php正确地重命名文件。

I have tried many suggested ways here on stack overflow but the file won't be moved either will be renamed as 0. a 我在堆栈溢出时尝试了许多建议的方法,但是该文件将不会被重命名为0。

It will be appreciated to let me know what is wrong with my code, please don't reply that I have to make my research first then write here, as I did already but I had no luck to figure it out myself. 让我知道我的代码有什么问题将不胜感激,请不要回信我必须先做研究然后再在这里写,就像我已经做过的一样,但是我没有运气自己弄清楚。

PHP 的PHP

$i = 0;
$extension = pathinfo($name, PATHINFO_EXTENSION);
$actual_name = pathinfo($name,PATHINFO_FILENAME);
$original_name = $actual_name;

while(file_exists('../cutomeruploads/'.$actual_name.".".$extension))
{           
$actual_name = (string)$original_name;
$actual_name = $actual_name.(string)$i;
$name = $actual_name.".".$extension;
$i++;
}


if(move_uploaded_file($_FILES['upl']['tmp_name'], '../cutomeruploads/'.$name)){
    $picname = $_FILES['upl']['name'];

    echo '{"status":"success"}';
    exit;
  }

您是否尝试过php的重命名功能?

rename("The existing file name", "the new name");

Renaming a file in php is quite easy. 在php中重命名文件非常容易。

rename("oldfile.ext","newname.ext");

For more info see this: http://us3.php.net/rename 有关更多信息,请参见: http : //us3.php.net/rename

Try this one. 试试这个。

/*
 * $dir - Directory path to check where the file is exist 
 * $filename - contains only name of the file
 * 
 */
public static function getFileName($dir, $filename) {

    // If name contains any white space replace with '-'
    $filename = str_replace(" ", "-", $filename);

    $filePath = $dir . $filename;

    $fileInfo = pathinfo($filePath);
    $i = 0;
    $flag = false;

    while(file_exists($filePath)) {
        $filePath = $dir . $fileInfo['filename'] . "_" . $i . "." . $fileInfo['extension'];
        $i++;
        $flag = true;
    }
    if($flag === TRUE)
        return $fileInfo['filename'] . "_" . $i . "." . $fileInfo['extension'];
    else
        return $fileInfo['filename'] . "." . $fileInfo['extension'];

}

This is will return new filename if already exist one. 如果已经存在,它将返回新的filename

For example, image.jpg already exist, it automatically adding image_1.jpg , image_2.jpg and so on. 例如, image.jpg已经存在,它将自动添加image_1.jpgimage_2.jpg等。

These changes renaming the file to xx1.ext xx2.ext 这些更改将文件重命名为xx1.ext xx2.ext

note:xx = $orDi 注意:xx = $ orDi

$actual_name = pathinfo($_FILES['upl']['name'], PATHINFO_FILENAME);
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
while(file_exists('../cutomeruploads/'.$actual_name.".".$extension))
{ 
 $i++;          
$actual_name = (string)$orDi.(string)$i;
}

if(move_uploaded_file($_FILES['upl']['tmp_name'], '../cutomeruploads/'.$actual_name.".".$extension)){
    $picname = $actual_name.".".$extension;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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