I am working on some PHP to rename photos in a directory to something else based on data from a CSV file. The CSV has three columns: Number, FirstName and LastName. The original names of the photos are "FirstName LastName.jpg". The new names should be "Number.jpg". I do this by cycling through the lines of the CSV, making the old name by putting FirstName and LastName together, making the new name by getting the number and renaming the files. However, I have two problems:
The first is more of a bug, but it might be significant. When I echo $oldname, there is a line break between the name and the dot before the extension, even though I have specified it as one string with no breaks.
The second is that it never works. It always returns "DID NOT rename whatever". I have changed the permissions of the files and containing folder but it still doesn't work. Thanks in advance if you can help.
<?php
$dir = "*Dir to phptos*";
$csv = fopen("filename.csv", "r") or die("no csv file");
//$ext = ".txt";
while(!feof($csv)) {
$line = fgets($csv);
$names = explode(",", $line);
//echo $names[2];
$oldname = $dir.$names[1]." ".$names[2].".txt";
$newname = $dir.$names[0].".txt";
if (is_file($oldname)) {
rename($oldname, $newname);
echo "renamed '".$oldname."' to '".$newname."'<br/>";
} else {
echo "DID NOT rename '".$oldname."'<br/>\n";
//echo "$oldname";
}
}
fclose($csv);
?>