James Williams
asked on
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
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);
?>
EE.php
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);
?>
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well understood Hielo,
Way to save my a55.
Thank you both.........
Selvol
Way to save my a55.
Thank you both.........
Selvol
Make me thing in a web virus... U_U
Anyway, here a snippet to rename all files in a directory:
Open in new window