blue-eye-labs
12-24-2007, 07:56 PM
The background to this problem is that I have a portfolio website, and I wish to be able to preload different parts of the site using a javascript preloader, which is held in a seperate js file, into which variables are passed such as the array holding the images, and the length of the preload bar.
If the images to be preloaded are from my portfolio, the url will reflect this "loadportfolio.php?gallery=images" and the like.
Thus I have another function in an external php file to deal with this, and another one to deal with images which will be read from a folder and passed into the array.
Here is my issue, I wrote a series of functions to do this, but I get lots of unexplicable (as far as my knowledge is concerned) debug messages. Here is the code:
<?php
//Function definition file
//Reads the contents of directories / subdirectories into an array
function read_directory_contents($dir) {
//set global variables
$read_files_files = array();
$read_files_directories = array();
//now read the files in the base directory
read_files($dir) or die("Error in function read_files() in function read_directory_contents()");
//now finish the directories
process_subdirectories($read_files_directories) or die("Error in function process_subdirectories() in function read_directory_contents()");
//DEBUG
echo "<pre> ***DEBUG MODE*** </pre>";
echo "<pre> ***DIR: " . $dir . " *** </pre>";
echo "<pre> ***DIRECTORIES*** </pre>";
echo "<pre>" . var_dump($read_files_directories) . "</pre>";
echo "<pre> ___DIRECTORIES___ </pre>";
echo "<pre> ***FILES*** </pre>";
echo "<pre>" . var_dump($read_files_files) . "</pre>";
echo "<pre> ___FILES___ </pre>";
//return vars
return $read_files_files;
}
function read_files($dir) {
//declare vars
global $read_files_files;
global $read_files_directories;
//open the directory
$d = opendir($_SERVER['DOCUMENT_ROOT'] . $dir) or die("Error processing directory $dir");
//loop through the files
while(($file = readdir($d)) !== false) {
if(!is_dir($file)) {
$read_files_files[] = $dir . "/" . $file;
} else {
//if it is a directory, then add that dir to the list
$read_files_directories[] = dirname($file);
}
}
echo "<pre> ***DIRECTORIES*** </pre>"; // *** DEBUG MODE ***
echo "<pre>" . var_dump($read_files_directories) . "</pre>"; // *** DEBUG MODE ***
echo "<pre> ___DIRECTORIES___ </pre>"; // *** DEBUG MODE ***
echo "<pre> ***FILES*** </pre>"; // *** DEBUG MODE ***
echo "<pre>" . var_dump($read_files_files) . "</pre>"; // *** DEBUG MODE ***
echo "<pre> ___FILES___ </pre>"; // *** DEBUG MODE ***
}
function process_subdirectories($subd) {
foreach($subd as $dir) {
//open the directory
$d = opendir($dir) or die("Unable to process directory $dir");
//now execute the read_files() function
read_files($d) or die("Error in function read_files() in function process_subdirectories()");
}
}
?>
Unfortunately, the output I get from this is as follows:
***DIRECTORIES***
array(2) { [0]=> string(1) "." [1]=> string(1) "." }
___DIRECTORIES___
***FILES***
array(14) { [0]=> string(25) "/library/images/.DS_Store" [1]=> string(29) "/library/images/403_error.png" [2]=> string(29) "/library/images/404_error.png" [3]=> string(22) "/library/images/_notes" [4]=> string(26) "/library/images/footer.png" [5]=> string(23) "/library/images/gid.jpg" [6]=> string(29) "/library/images/graphire3.png" [7]=> string(26) "/library/images/header.png" [8]=> string(30) "/library/images/madeonamac.jpg" [9]=> string(33) "/library/images/mainContentBG.png" [10]=> string(23) "/library/images/mbp.png" [11]=> string(26) "/library/images/menuBG.png" [12]=> string(26) "/library/images/newsBG.png" [13]=> string(24) "/library/images/wsBG.jpg" }
___FILES___
Error in function read_files() in function read_directory_contents()
And I really don't know why...
I would like to have this code working (which finds files, and loads them, so long as they aren't directories, in which case it adds the dir to a list and scans it next) so that I can just add images without worrying. I can get the first bit about images/anims/webdesigns working, I think, although if there are any errors throughout any help would be much appreciated.
Thank you.
If the images to be preloaded are from my portfolio, the url will reflect this "loadportfolio.php?gallery=images" and the like.
Thus I have another function in an external php file to deal with this, and another one to deal with images which will be read from a folder and passed into the array.
Here is my issue, I wrote a series of functions to do this, but I get lots of unexplicable (as far as my knowledge is concerned) debug messages. Here is the code:
<?php
//Function definition file
//Reads the contents of directories / subdirectories into an array
function read_directory_contents($dir) {
//set global variables
$read_files_files = array();
$read_files_directories = array();
//now read the files in the base directory
read_files($dir) or die("Error in function read_files() in function read_directory_contents()");
//now finish the directories
process_subdirectories($read_files_directories) or die("Error in function process_subdirectories() in function read_directory_contents()");
//DEBUG
echo "<pre> ***DEBUG MODE*** </pre>";
echo "<pre> ***DIR: " . $dir . " *** </pre>";
echo "<pre> ***DIRECTORIES*** </pre>";
echo "<pre>" . var_dump($read_files_directories) . "</pre>";
echo "<pre> ___DIRECTORIES___ </pre>";
echo "<pre> ***FILES*** </pre>";
echo "<pre>" . var_dump($read_files_files) . "</pre>";
echo "<pre> ___FILES___ </pre>";
//return vars
return $read_files_files;
}
function read_files($dir) {
//declare vars
global $read_files_files;
global $read_files_directories;
//open the directory
$d = opendir($_SERVER['DOCUMENT_ROOT'] . $dir) or die("Error processing directory $dir");
//loop through the files
while(($file = readdir($d)) !== false) {
if(!is_dir($file)) {
$read_files_files[] = $dir . "/" . $file;
} else {
//if it is a directory, then add that dir to the list
$read_files_directories[] = dirname($file);
}
}
echo "<pre> ***DIRECTORIES*** </pre>"; // *** DEBUG MODE ***
echo "<pre>" . var_dump($read_files_directories) . "</pre>"; // *** DEBUG MODE ***
echo "<pre> ___DIRECTORIES___ </pre>"; // *** DEBUG MODE ***
echo "<pre> ***FILES*** </pre>"; // *** DEBUG MODE ***
echo "<pre>" . var_dump($read_files_files) . "</pre>"; // *** DEBUG MODE ***
echo "<pre> ___FILES___ </pre>"; // *** DEBUG MODE ***
}
function process_subdirectories($subd) {
foreach($subd as $dir) {
//open the directory
$d = opendir($dir) or die("Unable to process directory $dir");
//now execute the read_files() function
read_files($d) or die("Error in function read_files() in function process_subdirectories()");
}
}
?>
Unfortunately, the output I get from this is as follows:
***DIRECTORIES***
array(2) { [0]=> string(1) "." [1]=> string(1) "." }
___DIRECTORIES___
***FILES***
array(14) { [0]=> string(25) "/library/images/.DS_Store" [1]=> string(29) "/library/images/403_error.png" [2]=> string(29) "/library/images/404_error.png" [3]=> string(22) "/library/images/_notes" [4]=> string(26) "/library/images/footer.png" [5]=> string(23) "/library/images/gid.jpg" [6]=> string(29) "/library/images/graphire3.png" [7]=> string(26) "/library/images/header.png" [8]=> string(30) "/library/images/madeonamac.jpg" [9]=> string(33) "/library/images/mainContentBG.png" [10]=> string(23) "/library/images/mbp.png" [11]=> string(26) "/library/images/menuBG.png" [12]=> string(26) "/library/images/newsBG.png" [13]=> string(24) "/library/images/wsBG.jpg" }
___FILES___
Error in function read_files() in function read_directory_contents()
And I really don't know why...
I would like to have this code working (which finds files, and loads them, so long as they aren't directories, in which case it adds the dir to a list and scans it next) so that I can just add images without worrying. I can get the first bit about images/anims/webdesigns working, I think, although if there are any errors throughout any help would be much appreciated.
Thank you.