<?php /** * Lab 03, Exercise 3 & 4 — Start File * @author Bramus Van Damme <*****@*****.**> */ // vars $basePath = __DIR__ . DIRECTORY_SEPARATOR . 'images'; // C:\wamp\www\vn.an\labo03\images $baseUrl = 'images'; // images $images = array(); // An array which will hold all our images $lines = new SPLFileObject($basePath . DIRECTORY_SEPARATOR . 'captions.txt'); // Main code // @TODO Open directory and captions file using some SPL classes $di = new DirectoryIterator($basePath); // @TODO loop directory foreach ($di as $file) { // exclude . and .. + we don't want directories if (!$file->isDot() && !$file->isDir()) { // If it's a '.jpg', add it onto an array named $images // Use an associative array so you can store the filename, and caption if ($file->getExtension() === 'jpg') { $images[] = array('url' => $baseUrl . DIRECTORY_SEPARATOR . $file, 'caption' => $lines->current()); $lines->next(); } } } ?> <!doctype html>
<?php /** * Lab 03, Exercise 4 — Solution * @author Bramus Van Damme <*****@*****.**> */ // vars $basePath = __DIR__ . DIRECTORY_SEPARATOR . 'images'; // C:\wamp\www\vn.an\labo03\images $baseUrl = 'images'; // images $images = array(); // Main code // Open dir and captions $di = new DirectoryIterator($basePath); $captions = new SPLFileObject($basePath . DIRECTORY_SEPARATOR . 'captions.txt'); // loop directory foreach ($di as $file) { // We only want .jpg if ($file->getExtension() == 'jpg') { // get the caption $caption = $captions->current(); // store the image array_push($images, array('url' => 'images/' . $file, 'caption' => str_replace(PHP_EOL, '', $caption))); // move captions pointer to next caption $captions->next(); } } ?> <!doctype html> <html>