PHP重命名文件夹

时间:2010-10-13 13:17:09

标签: php

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

我希望你理解我的意思。

编辑:分类只是textlinks

      
  • ODLA
  •   
  • 第2类

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

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

4 个答案:

答案 0 :(得分:8)

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

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

修改

示例:

<?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);

?>

请检查我这个?

答案 1 :(得分:1)

您可能需要查看iconvAnother stack question

答案 2 :(得分:0)

假设您已经获得了服务器上的文件夹列表(通过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);
}

它相当邋,,但它也相当简单。如果你想要更快速运行的东西,你会看到数学或与unicode的二进制值进行比较。例如,$o数组中的所有内容都是unicode字符00D2到00D6和00F2到00F6。因此,如果该字母位于dechex('00D2')dechex('00D6')之间,或该字母位于dechex('00F2')dechex('00F6')之间,则将其替换为“o”。

如果您获得的值没有特殊字符(例如通过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);
?>

答案 3 :(得分:0)

我读了@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>";
?>