Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP rename folder

Tags:

php

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.

I hope you understand what I mean.

Edit: The categorys is only textlinks

  • Ödla
  • Category 2

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

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

like image 609
Boo Avatar asked Oct 13 '10 13:10

Boo


Video Answer


3 Answers

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.

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?

like image 199
Etienne Marais Avatar answered Oct 05 '22 09:10

Etienne Marais


You might want to look at iconv, Another stack question

like image 37
Phill Pafford Avatar answered Oct 05 '22 10:10

Phill Pafford


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:

$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 $o array 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".

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 odla could only refer to one of 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.

<?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);
?>
like image 45
stevendesu Avatar answered Oct 05 '22 09:10

stevendesu