Rename files using php - filenames in text file
I have a list of file names in a text file and i have been reading the
text file and using the vales to rename files in the folder. Example of
the names in the text file, each name in the file represents two images
(front and back).
12455 124788 12145
Basically we have duplex scanned a number of images, scanner gives it a
default name of 1, 2, 3 etc and we would like to rename it to the code on
the file, so the codes are entered in the text file and the rename program
will rename the files accordingly
Problem i am getting is it is only renaming the last two images in the
folder. Here is the code thanks for the help
<?php
$second_file = "n";
$do_second = "n";
$i = 0;
$count = 0;
if ($handle = opendir('C:\xampp\htdocs\test')) {
$a = 0;
echo "Directory handle: $handle\n";
echo "Files:\n";
$file = fopen("num.txt","r") or exit ("Unable to open file num.txt");
while (!feof($file)){
$data = fgets($file);
$data2 = substr($data,0,9);
$num[$i] = $data2;
$i = $i + 1;
$count = $count + 1;
}
$a = 0;
while (false !== ($file2 = readdir($handle))) {
$filename[$a] = $file2;
$a = $a + 1;}
sort($filename,SORT_NUMERIC);
$count2 = 0;
for($a=2; $a <sizeof($filename); $a++){
$value = $num[$count2];
$test = $filename[$a];
$temp2 = substr($filename[$a],0,1);
if ($temp2 !== "."){
if ($second_file =="n") {
$temp = substr($filename[$a], 0, 1);
if($temp !== "R") {
rename("/xampp/htdocs/test/$test","/xampp/htdocs/test/R1
- $value - front.JPG");
$second_file = "y";
$do_second = "n";
print "First file no: $value <br>";
}
}
if ($second_file =="y") {
$temp = substr($filename[$a], 0, 1);
if($temp !== "R") {
if($do_second =="y"){
rename("/xampp/htdocs/test/$test","/xampp/htdocs/test/R1 -
$value - back.JPG");
$second_file = "n";
$count2 = $count2 + 1;
print "Second file yes: $value <br>";
}
if($do_second =="n"){
$do_second = "y";
}
}
}
}
}
fclose($file);
closedir($handle);
print "Rename complete"; }
No comments:
Post a Comment