Click to See Complete Forum and Search --> : Help with PHP


lomfs24
03-16-2003, 10:38 AM
I have a simple stupid quetion that I have been fighting with for two days now. All I need to do is get a script that will look in a directory for files or a specific kind of file, like a '.pdf' file and return the contents of the directory in an array. I have fought with it for two days. I can use readdir() and get it to work but the results are not in an array so I can't sort them alpha-numerically.
Here is a copy of the script that I use for a readir() function

<?
$dirname = "/path/to/httpdocs/newsletters";
$dh = opendir($dirname) or die ("couldn't open directory");
while (!(($file = readdir($dh)) === false) ) {
if (strpos($file,'.pdf') ) {
$base = basename($file, '.pdf');
echo "<a HREF='http://www.mydomain.com/newsletters/$file'>$base<br></a>";
}
elseif (strpos($file,'.PDF') ) {
echo "<a HREF='http://www.mydomain.com/newsletters/$file'>$file<br></a>";
}
}
closedir($dh);
?>


This script works fine but I cannot sort alpha-numerically.
Thanks for all your help,
You can see this script in action at www.lionsdistrict37a.org/news.php

jeffmott
03-16-2003, 11:49 AM
See the glob (http://www.php.net/manual/en/function.glob.php) function. It does exactly what you want.

lomfs24
03-17-2003, 12:41 AM
I went to php.net and looked at the glob function. None of the books that I have mention it and the tried to use it exactly the way they were using it at php.net. I get a "Error, call to undefined funtion glob() in "filemene line X"

Maybe my version of PHP is too old but it is the version that came with Redhat 8!?!?!?!?

The glob() function doesn't work on my real world server either. I get the same error.

If you can think of anything else I would be happy to hear about it.

PS Here is how I used the glob() function

<?php
foreach(glob("newsletters/*.pdf") as $filename) {
echo "$filename\n";
}
?>

Now I know that script isn't written perfectly but I still get an undefined function error.

I will also try searching google for the function to see if I can get a better explanation.

pyro
03-17-2003, 08:39 AM
You may want to try something like this to put the files into an array:

$folder = "yourfolder";
$handle = opendir($folder);
while ($file = readdir($handle))
{
$array[] = $file;
}
closedir($handle);

and then you can sort your array with sort($array)

lomfs24
03-17-2003, 04:54 PM
Thanks pyro. I have used the script that you gave me for a base on what I am doing. Of course I modified it a little to make it fit my needs specifically, like I added a strpos() function to search for specific file types and I added a basename() function to strip off the file extension to the display. But you certainly got me on the right track. Thanks a million.

jeffmott
03-17-2003, 05:40 PM
Pyro, I'm curious if you had an explanation for why the documented glob function didn't work properly?

pyro
03-17-2003, 08:01 PM
My guess would be that lomfs24 is using an older version of PHP. I believe that glob was introduced in PHP 4.3, so that could be the explaination...

lomfs24
03-18-2003, 01:21 AM
I am not sure what version of PHP I am running. I have a newer version installed on my laptop but I am mainly developing on my Linux machine which is running Redhat 8 with all of the newest updates. It is whatever version comes with that and I am not too sure which version it is or how to check it.

lomfs24
03-18-2003, 01:26 AM
I am also developing on my server at home and when i get all the bugs worked out of a script and get it to do what I want I then upload it to my real server. The other server does not support the glob() function either so I would rather develop without it.

lomfs24
03-18-2003, 01:29 AM
I just ran the phpinfo() function and at the top it says version 4.2.2 so you are right, I am running a version prior to 4.3.
OK, I will shut up now.