PHP重命名文件夹

Boo*_*Boo 5 php

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

我希望你明白我的意思.

编辑:类别只是文本链接

  • ODLA
  • 第2类

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

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

Eti*_*ais 9

你总是可以使用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);

?>
Run Code Online (Sandbox Code Playgroud)

请问我这个问题?


ste*_*esu 2

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

\n\n
$a = array(...);\n$e = array(...);\n$i = array(...);\n$o = array(\'\xc3\x92\',\'\xc3\x93\',\'\xc3\x94\',\'\xc3\x95\',\'\xc3\x96\',\'\xc3\xb2\',\'\xc3\xb3\',\'\xc3\xb4\',\'\xc3\xb5\',\'\xc3\xb6\');\n$u = array(...);\nforeach($folderList as $folder){\n    $folder = str_replace($a,"a",$folder);\n    $folder = str_replace($e,"e",$folder);\n    $folder = str_replace($i,"i",$folder);\n    $folder = str_replace($o,"o",$folder);\n    $folder = str_replace($u,"u",$folder);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n
<?php\n    $vowels = array("a", "e", "i", "o", "u");\n    $replace = array("...", "...", "...", "{\xc3\x92,\xc3\x93,\xc3\x94,\xc3\x95,\xc3\x96,\xc3\xb2,\xc3\xb3,\xc3\xb4,\xc3\xb5,\xc3\xb6}", "...");\n\n    $search = "odla";\n    $search = str_replace($vowels, $replace, $search);\n\n    // Returns every folder who matches "\xc3\x92dla", "\xc3\x93dla", "\xc3\x94dla"....\n    glob($search, GLOB_BRACE);\n?>\n
Run Code Online (Sandbox Code Playgroud)\n