• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

PHP rename all files in a DIR using a variable

The following script works fine. When attached to a webpage it will seek the "meta description" then make a copy of its self named with the meta info it has found.

What I have been attempting to do is run this script and have it rename all files in a folder.   The hard part   has been getting the script to read each file and rename it using the meta description from that file.

Basically as opposed to adding the script to each file and running the page does anyone know how to just run the code and have it process all the pages in the DIR as if the php script was included in that file?

Apologies if what I need is not clear.
Attached is a file . When ran the php will make a copy of that file with. Renamed using the meta description and a random number.      
I would like to have it rename all the file in the DIR the same way.

Thank you in advance you assistance is greatly appreciated.  

Selvol


<?php
srand ((double) microtime( )*1000000);
$random_number = rand(0,100);
$str = file_get_contents(__FILE__);
        $aa = "--";
         $ab = "_";
        preg_match('%<meta name=\"description\" content=\"([^"]*)"%',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[1].$random_number.$ab.$namesArray[0].$aa.$namesArray[3].$ab.$namesArray[2].".php","w+");
        fwrite($file,$str);
         preg_match('%<?"([^"]*)"%',$str,$matchArray);
        fclose($file);
?>

Open in new window

EE.php
0
selvol
Asked:
selvol
  • 2
  • 2
1 Solution
 
VanHackmanCommented:
WowWowWow... "copy of its self"?, "rename all the file in the DIR?"...
Make me thing in a web virus... U_U

Anyway, here a snippet to rename all files in a directory:



<?php

$Directory = "your/path";
$Prefix    = "Prefix-";

$Dir = opendir($Directory); 

while ($File = readdir($Dir))
{

   if ($file != '.' && $file != '..' && ($file != pathinfo($_SERVER['PHP_SELF'],PATHINFO_BASENAME)))
   { 
      rename($path."/".$file, $path."/".$Prefix.$file);
   } 

}

closedir($Dir); 

?>

Open in new window

0
 
VanHackmanCommented:
Sorry.. I write a "file" as "File", here the code fixed:
<?php

$Directory = "your/path";
$Prefix    = "Prefix-";

$Dir = opendir($Directory); 

while ($file = readdir($Dir))
{

   if ($file != '.' && $file != '..' && ($file != pathinfo($_SERVER['PHP_SELF'],PATHINFO_BASENAME)))
   { 
      rename($path."/".$file, $path."/".$Prefix.$file);
   } 

}

closedir($Dir); 

?>

Open in new window

0
 
selvolAuthor Commented:
Hello, thank you for the reply....    I may  be wrong but.


I need the rename to use the information it get from the "meta description"  in each file.


So a set prefix is not going to work.


THank you
0
 
hieloCommented:
try:
<?php
//assuming you want to replace all the file at http://www.yoursite.com/include
$Directory=$_SERVER['DOCUMENT_ROOT'] . "/include";
$Dir = opendir($Directory); 

while ($file = readdir($Dir))
{

   if ($file != '.' && $file != '..' && ($file != pathinfo($_SERVER['PHP_SELF'],PATHINFO_BASENAME)))
   { 
       renameFile($file);
   } 
}

closedir($Dir); 


function renameFile($fileName)
{
	srand ((double) microtime( )*1000000);
	$random_number = rand(0,100);
	$str = file_get_contents($fileName);
        $aa = "--";
         $ab = "_";
        preg_match('%<meta name=\"description\" content=\"([^"]*)"%',$str,$matchArray);
        $namesArray = explode(",",$matchArray[1]);
        $file = fopen($namesArray[1].$random_number.$ab.$namesArray[0].$aa.$namesArray[3].$ab.$namesArray[2].".php","w+");
        fwrite($file,$str);
         preg_match('%<?"([^"]*)"%',$str,$matchArray);
        fclose($file);
}
?>

Open in new window

0
 
selvolAuthor Commented:
Well understood Hielo,
Way to save my a55.








Thank you both.........
Selvol


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now