Click to See Complete Forum and Search --> : File/directory reading issues


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.

scragar
12-25-2007, 12:59 AM
you are aware that the function read_files doesn't return anything, thus meaning that it effectivly returnes undefined, a value that also happens to evaluate to false, causing your error, right?

blue-eye-labs
12-25-2007, 06:34 AM
you might have a point there (slaps himself hard)...
I think I may have tried it before but... ok, that makes it work, thanks (the reason I hadn't returned anything is because I thought all my variables were global)... my issue now is that I can't get the subdirectories processed, which is annoying... but I think I've got everything else working
Here is the updated code of the read_directories file:


<?php
//Function definition file
//Reads the contents of directories / subdirectories into an array


function read_directory_contents($dir) {
//set global variables
global $read_files_files;
global $read_files_directories;
//now read the files in the base directory
$read_files_files = read_files($dir) or die("Error in function read_files() in function read_directory_contents()");
//now finish the directories
//$read_files_files .= process_subdirectories($read_files_directories) or die("Error in function process_subdirectories() in function read_directory_contents()");

//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) && $file != "_notes" && $file != ".DS_Store" && $file != ".htaccess") { //just need to make sure that no system/dreamweaver files are found
$read_files_files[] = $dir . "/" . $file;
} else {
//if it is a directory, then add that dir to the list
$read_files_directories[] = $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 ***
*/

return $read_files_files;
}

function process_subdirectories($subd) {
foreach($subd as $dir) {
//globals
global $read_files_files;
global $read_files_directories;
//open the directory
$d = opendir($dir) or die("Unable to process directory $dir");
//now execute the read_files() function
$read_files_files .= read_files($d) or die("Error in function read_files() in function process_subdirectories()");
//return
return $read_files_files;
}
}
?>


Here's how it is called:

if($gall == "images" || "animations" || "webdesign") {
$a = load_gallery($gall);
} else {
$a = read_directory_contents($DIR_images);
}


Thanks for pointing out that stupid mistake, and have a merry xmas and a happy new year.

scragar
12-25-2007, 07:23 PM
if($gall == "images" || "animations" || "webdesign") {you've got a problem right there, what your asking there is not "if the gall is one of the three: images, animations or webdesign" what your asking is "if the gall is images, or animations evaluates to true(which it does), or webdesign evaluates to true(another true)", rendering the whole if useless.

you have 2 choices, use a more explanitory if, or switch. both shown below.
notes: switch is slightly faster, but if uses less space and I'd assume makes more sense to you.
if($gall == "images" || $gall == "animations" || $gall == "webdesign") {
$a = load_gallery($gall);
} else {
$a = read_directory_contents($DIR_images);
}
switch($gall){
case "images":
case "animations":
case "webdesign":
$a = load_gallery($gall);
break;
default:
$a = read_directory_contents($DIR_images);
break;
};

NOTE not looked into it much more, spoted this and thought it relevant to point out immediatly

NogDog
12-26-2007, 01:39 AM
Another alternative to the issue Scragar raised above is to use in_array():

if(in_array($gall, array('images', 'animation', 'webdesign')))
{
// do stuff....
}