i'm using this function to call up and include a random file. it only works every other call, which leads me to conclude that i need to reset it somehow. any pointers?
<?php
function getRandomFile($start_dir)
{
chdir($start_dir);
$dir = opendir('.');
while (($myfile = readdir($dir)) !==false)
{
if ($myfile != '.' && $myfile != '..' && is_file("$myfile") &&
$myfile != 'resource.frk')
{
$files[] = $myfile;
}
}
closedir($dir);
chdir('../');
srand ((float) microtime() * 10000000);
$file = array_rand($files);
return $files[$file];
}
// and now we call the function...
$randomfile = getRandomFile("blurbs/blurb1");
include("http://localhost/newvat/blurbs/blurb1/{$randomfile}");
?>thanks in advance!I don't know fo anyway to reset a function, but you can start off with a empty $file array. just create the array right at the top of the function.i've tried unsetting and setting to null both $randomfile and $file, but still i get the same thing.
i'm calling the function about 5 times, and every other one (1-3-5) works. the other times, i get something likeWarning: chdir(): No such file or directory (errno 2) in W:\www\newvat\functions\randominclude.php on line 6
Warning: array_rand(): First argument has to be an array in W:\www\newvat\functions\randominclude.php on line 19
Index of /newvat/blurbs/blurb2
Icon Name Last modified Size Description[DIR] Parent Directory -
[ ] test1.blurb 09-Jul-2004 00:14 14
[ ] test2.blurb 09-Jul-2004 00:15 10
[ ] test3.blurb 09-Jul-2004 00:15 35
[ ] test4.blurb 09-Jul-2004 00:15 9
[ ] test5.blurb 09-Jul-2004 00:15 20
Apache/2.0.48 (Win32) PHP/4.3.4 Server at localhost Port 80'resource.frk'
You using a mac?no, but a good chunk of that code is from a how-to article i found, and i never did bother to take out that argument. it'll be replaced by another filename here soon.ahhh, resource forks on mac's are how it knows what program to open it with, its named differently across the network.I don't think you need chdir($start_dir); as all you have to do is open the directory.Originally posted by scoutt
I don't think you need chdir($start_dir); as all you have to do is open the directory. yeah, but i want to specify a different directory each time, if necessary.then give it the ful directory, you all ready do this
getRandomFile("blurbs/blurb1");
opendir will open anything you send to it, no reason to do chdir.
well apparently if you get errors on chdir then take it out and send the path to it like you already are.er... when i change chdir to opendir, i get errors on ALL of the function calls.a-ha! you're right. somebody else online pointed me to the same chdir thing and posted this code instead:function getRandomFile($directory)
{
$filesarr = array();
if($dir_handle = opendir($directory)){
while($file = readdir($dir_handle))
{
if($file !== "." && $file !== ".." && $file != 'template.blurb') {
$filesarr[] = $file;
}
}
}
return $filesarr[rand(0, (count($filesarr) - 1))];
}and it works exactly as intended now. thanks for all the help!Originally posted by transmothra
a-ha! you're right.
like you had any doubt??? nah, just kidding, glad it worked out for ya
<?php
function getRandomFile($start_dir)
{
chdir($start_dir);
$dir = opendir('.');
while (($myfile = readdir($dir)) !==false)
{
if ($myfile != '.' && $myfile != '..' && is_file("$myfile") &&
$myfile != 'resource.frk')
{
$files[] = $myfile;
}
}
closedir($dir);
chdir('../');
srand ((float) microtime() * 10000000);
$file = array_rand($files);
return $files[$file];
}
// and now we call the function...
$randomfile = getRandomFile("blurbs/blurb1");
include("http://localhost/newvat/blurbs/blurb1/{$randomfile}");
?>thanks in advance!I don't know fo anyway to reset a function, but you can start off with a empty $file array. just create the array right at the top of the function.i've tried unsetting and setting to null both $randomfile and $file, but still i get the same thing.
i'm calling the function about 5 times, and every other one (1-3-5) works. the other times, i get something likeWarning: chdir(): No such file or directory (errno 2) in W:\www\newvat\functions\randominclude.php on line 6
Warning: array_rand(): First argument has to be an array in W:\www\newvat\functions\randominclude.php on line 19
Index of /newvat/blurbs/blurb2
Icon Name Last modified Size Description[DIR] Parent Directory -
[ ] test1.blurb 09-Jul-2004 00:14 14
[ ] test2.blurb 09-Jul-2004 00:15 10
[ ] test3.blurb 09-Jul-2004 00:15 35
[ ] test4.blurb 09-Jul-2004 00:15 9
[ ] test5.blurb 09-Jul-2004 00:15 20
Apache/2.0.48 (Win32) PHP/4.3.4 Server at localhost Port 80'resource.frk'
You using a mac?no, but a good chunk of that code is from a how-to article i found, and i never did bother to take out that argument. it'll be replaced by another filename here soon.ahhh, resource forks on mac's are how it knows what program to open it with, its named differently across the network.I don't think you need chdir($start_dir); as all you have to do is open the directory.Originally posted by scoutt
I don't think you need chdir($start_dir); as all you have to do is open the directory. yeah, but i want to specify a different directory each time, if necessary.then give it the ful directory, you all ready do this
getRandomFile("blurbs/blurb1");
opendir will open anything you send to it, no reason to do chdir.
well apparently if you get errors on chdir then take it out and send the path to it like you already are.er... when i change chdir to opendir, i get errors on ALL of the function calls.a-ha! you're right. somebody else online pointed me to the same chdir thing and posted this code instead:function getRandomFile($directory)
{
$filesarr = array();
if($dir_handle = opendir($directory)){
while($file = readdir($dir_handle))
{
if($file !== "." && $file !== ".." && $file != 'template.blurb') {
$filesarr[] = $file;
}
}
}
return $filesarr[rand(0, (count($filesarr) - 1))];
}and it works exactly as intended now. thanks for all the help!Originally posted by transmothra
a-ha! you're right.
like you had any doubt??? nah, just kidding, glad it worked out for ya