php PHP重命名文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3924049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:28:25  来源:igfitidea点击:

PHP rename folder

php

提问by Boo

I have a photo gallery with a simple ul list of categories. The categories are folders on my server. If a folder is named "?dla" then the category is named "?dla" too. Is it possible to replace "?" in "?dla" with "o" only on the category and not the actual folder on the server. I don′t want to use mysql.

我有一个带有简单 ul 类别列表的照片库。类别是我服务器上的文件夹。如果文件夹被命名为“?dla”,那么该类别也被命名为“?dla”。可以替换“?” 在“?dla”中,“o”只在类别上,而不是服务器上的实际文件夹。我不想使用mysql。

I hope you understand what I mean.

我希望你明白我的意思。

Edit: The categorys is only textlinks

编辑:类别只是文本链接

  • ?dla
  • Category 2
  • ?dla
  • 类别 2

And in this example there are two folder on my server named "?dla" and "Category 2"

在这个例子中,我的服务器上有两个名为“?dla”和“Category 2”的文件夹

I just want to rename the category links, not the folder.

我只想重命名类别链接,而不是文件夹。

回答by Etienne Marais

You could always use the PHP function rename(). Using @steven_desu's str_replace method you can call the rename of the old folder with the new name. Have a look at the documentation.

您可以随时使用 PHP 函数rename()。使用@steven_desu 的 str_replace 方法,您可以使用新名称调用旧文件夹的重命名。看看文档。

http://www.php.net/manual/en/function.rename.php

http://www.php.net/manual/en/function.rename.php

EDIT

编辑

example :

例子 :

<?php
// Create arrays with special chars
$o = array('ò','ó','?','?','?','ò','ó','?','?','?');
// Remember to remove the slash at the end otherwise it will not work
$oldname = '/path/to/directory/?dla';

// Get the directory name
$old_dir_name = substr($oldname, strrpos($oldname, '/') + 1);

// Replace any special chars with your choice
$new_dir_name = str_replace($o, 'O', $old_dir_name);

// Define the new directory
$newname = '/path/to/new_directory/' . $new_dir_name;

// Renames the directory
rename($oldname, $newname);

?>

Please just check me on this?

请检查我这个?

回答by Phill Pafford

You might want to look at iconv, Another stack question

你可能想看看iconv另一个堆栈问题

回答by stevendesu

Assuming you've already gotten a list of folders on the server (via while()loop or glob()function) and this list is stored in $folderList[], I'd try something like the following if you're just outputting the result:

假设您已经在服务器上获得了一个文件夹列表(通过while()循环或glob()函数)并且该列表存储在 中$folderList[],如果您只是输出结果,我会尝试如下操作:

$a = array(...);
$e = array(...);
$i = array(...);
$o = array('ò','ó','?','?','?','ò','ó','?','?','?');
$u = array(...);
foreach($folderList as $folder){
    $folder = str_replace($a,"a",$folder);
    $folder = str_replace($e,"e",$folder);
    $folder = str_replace($i,"i",$folder);
    $folder = str_replace($o,"o",$folder);
    $folder = str_replace($u,"u",$folder);
}

It's fairly sloppy, but it's also fairly simple. If you wanted something that would run quicker you'd be looking at doing math or comparisons with the binary value of the unicode. For instance, everything in the $oarray is the unicode characters 00D2 to 00D6 and 00F2 to 00F6. So if the letter is between dechex('00D2')and dechex('00D6')OR that letter is between dechex('00F2')and dechex('00F6')then replace it with "o".

这相当草率,但也相当简单。如果你想要运行得更快的东西,你会考虑用 unicode 的二进制值进行数学运算或比较。例如,$o数组中的所有内容都是 Unicode 字符 00D2 到 00D6 和 00F2 到 00F6。因此,如果字母介于dechex('00D2')和之间dechex('00D6')或该字母介于两者之间dechex('00F2')dechex('00F6')然后将其替换为“o”。

If you're getting the value without the special characters (such as via URL post) and you want to map it to a folder, then you have to take a slightly different approach. First, realize that this isn't the ideal solution since you may have two folders, one named òdla, one named ?dla. In this case, the search phrase odlacould only refer to oneof these folders. The other would be permanently ignored. Assuming you're fine with this (such as: you can guarantee that there will be no such duplicate folder names), you would probably want to use GLOB_BRACE.

如果您获取的值没有特殊字符(例如通过 URL 发布),并且您想将其映射到文件夹,那么您必须采用稍微不同的方法。首先,意识到这不是理想的解决方案,因为您可能有两个文件夹,一个名为òdla,一个名为?dla. 在这种情况下,搜索短语odla只能是指一个这些文件夹中。另一个将被永久忽略。假设您对此没有问题(例如:您可以保证不会有此类重复的文件夹名称),您可能希望使用GLOB_BRACE.

<?php
    $vowels = array("a", "e", "i", "o", "u");
    $replace = array("...", "...", "...", "{ò,ó,?,?,?,ò,ó,?,?,?}", "...");

    $search = "odla";
    $search = str_replace($vowels, $replace, $search);

    // Returns every folder who matches "òdla", "ódla", "?dla"....
    glob($search, GLOB_BRACE);
?>

回答by Boo

I read what @steven_desu wrote and think it looks interesting, but I′m not sure how to use it. I′ve tried with this:

我读了@steven_desu 写的东西,觉得它看起来很有趣,但我不知道如何使用它。我试过这个:

<?php
function fixlink($link){
    $match = array('?','?','?','?','?','?',' ');
    $replace = array('a','a','o','a','a','o','-');
    return str_replace($match, $replace, $link);
}
function matchlink($search){
    $vowels = array("a", "e", "i", "o", "u");
    $replace = array("...", "...", "...", "{ò,ó,?,?,?,ò,ó,?,?,?}", "...");

    $search = "odla";
    $search = str_replace($vowels, $replace, $search);

    // Returns every folder who matches "òdla", "ódla", "?dla"....
    glob($search, GLOB_BRACE);
}
echo "<a href=\"index.php?page=".matchlink(fixlink($file))."\">".$file."</a>";
?>