Can't understand a piece of code
Posted: Thu Aug 07, 2014 7:51 am
I am having trouble understanding how a part in this 'while' piece of code works. I hope that i have given enough of the code to make sense.
I understand that while currentFile is false (or still has more files to go through in the directory) that the code will keep going - So, currentFile lands up holding all the info for the directory, and is then broken up into an array using $theFiles[]?
If i add a foreach, will it show all theFiles (obviously it is set to display image files, but the file names can be shown just to see if it is working); what will this foreach piece of code look like - I have tried to create one but it didn't work:
I understand that while currentFile is false (or still has more files to go through in the directory) that the code will keep going - So, currentFile lands up holding all the info for the directory, and is then broken up into an array using $theFiles[]?
If i add a foreach, will it show all theFiles (obviously it is set to display image files, but the file names can be shown just to see if it is working); what will this foreach piece of code look like - I have tried to create one but it didn't work:
Code: Select all
foreach($theFiles as $testFiles){
print $testFiles;
}
Code: Select all
...
$dirName = "images/";
$dp = opendir($dirName);
chdir($dirName);
//add all files in directory to $theFiles array
while ($currentFile !== false){
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
} // end while
//extract gif and jpg images
$imageFiles = preg_grep("/jpg$|gif$/", $theFiles);
...