简体   繁体   English

在PHP中使用重命名功能

[英]Working with rename function in PHP

There is a file within the directory and I am trying to use rename( arg1, arg2 ) to rename the arg1 file. 目录中有一个文件,我试图使用重命名(arg1,arg2)来重命名arg1文件。

However, the arg1 file contains Asian letters and I get the message that the file is not available. 但是,arg1文件包含亚洲字母,我收到该文件不可用的消息。

how can I solve this issue 我该如何解决这个问题

thanks 谢谢

    $elements = scandir($dir);


foreach ($elements as $key => $value) 
{

    rename("./$value", "$newname");

}

Be sure to put the paths correctly: 一定要正确放置路径:

$elements = scandir($dir);
foreach ($elements as $key => $value) {
    rename($dir.'/'.$value, $dir.'/'.$newname);
}

To be less abstract, assume $dir is something like "/home/someuser/somefiles" and your script is located at "/var/www/script.php", you are getting all files (eg "oldname.txt") from scandir($dir), so the absolute path to the file is "/home/someuser/somefiles/oldname.txt", but you are passing "./oldname.txt" to the rename function, which is in fact "/var/www/oldname.txt" 为了减少抽象,假设$ dir类似于“/ home / someuser / somefiles”,并且您的脚本位于“/var/www/script.php”,您将从中获取所有文件(例如“oldname.txt”) scandir($ dir),所以文件的绝对路径是“/home/someuser/somefiles/oldname.txt”,但你将“./oldname.txt”传递给重命名函数,实际上是“/ var” /www/oldname.txt”

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

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