Your Location is: Home > Php

Rename multi text file based on content in php

From: Japan View: 4204 kamrul islam 
 php

Question

I have a few hundred text files (of various names) in a folder.

ie: in TextFile01.txt

line 1
line 2 < rename this file based on this line
line 3 
line 4

i'm try this

$dir = "temp"; //folder which has all downloaded images

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "Renaming file:".$dir."/".$file."<br />";
              $file_name_old=$dir."/".$file;
$new = fgets($file);
              $file_name_new = $new[1].".txt"; //adding extension to file
            rename($file_name_old, $dir."/".$file_name_new); 
        }
        closedir($dh);
    }
}

Best answer