I'm trying to write this program which will copy some files in all subdirectories of a folder.
The file will copied if:
1. It has the extension .jpg
2. It is the only jpg in the folder
The idea is that if any single jpg in a subdirectory exists it will be copied to the same subdirectory on renamed to Front.jpg
The parent directory is "My Albums", my code so far creates an array of all the directories (and files if there were any) in that folder.
It then uses a foreach loop to open each directory and look for jpg's. My problem is counting the number of jpg's in each subfolder to determine if it's the only one.
Here's my code so far:
Code: Select all
<?php
function buildmp3() {
$dirarray = array();
$handle = opendir ('c:\My Albums');
while ($folder = readdir($handle)) { /*Get contents of directory*/
$dirarray[$folder] = $folder;
}
return $dirarray;
}
$dirarray = buildmp3(); /*Create the array of folder contents from the function above*/
foreach ($dirarray as $n => $v) {
if ($n !="." && $n != "..") { /*Ignore .. and . directories*/
$handle2 = opendir ('c:\my albums\''.$n.''); /*Open subfolders*/
while ($contents = readdir ($handle2)) {
if (substr($contents, -3) == "jpg") {
/*I can't work out where to go from here*/
}
}
}
}
?>Thanks