View Full Version : List files without OPENDIR ?

01-14-2008, 12:59 PM
Hi folks,

Is there any way to list the files in a directory without using the OPENDIR function?

I cannot use OPENDIR because the server is running in safe mode:

Warning: opendir() [function.opendir]: SAFE MODE Restriction in effect. The script whose uid is 594 is not allowed to access /home/.sites/120/site35/web owned by uid 99

Thanks for your help !

01-14-2008, 01:12 PM
try scandir or glob

01-14-2008, 01:55 PM
Thanks for your answer.

The scandir function gives me the same error (SAFE MODE).
But the glob should be fine for what I need.

Thanks again, topic closed! :thumbsup:

01-17-2008, 03:31 PM
I am re-using my old post because my new question is still related to the same issue.

Can the glob() function be used to list files from a remote location?

I would like to list *.csv files from another website: http://www.remotesite.com/folder/*.*

This remote website is not on the same server !

I tried the following without success:

$path = "http://www.remotesite.com/folder/";
$files = "*.csv";

foreach (glob($path.$files) as $filename) {
echo $filename."\n";

Any idea?
Is there another function I could use to achieve what I want?
Thanks in advance!

01-17-2008, 03:51 PM
Well... I found this little script that does the job as expected:

$matches = array();

preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://www.remotesite.com/folder/'), $matches);

foreach($matches[2] as $match)
echo $match . '<br>';

function get_text($filename)
$fp_load = fopen("$filename", "rb");

if ( $fp_load )
while ( !feof($fp_load) )
$content .= fgets($fp_load, 8192);

return $content;

Is that the best way to do it? If anybody has a better way, please let me know!

01-17-2008, 03:52 PM
Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.

Directly from the PHP website.
For remote access, try something like the ftp extensions.

01-17-2008, 03:59 PM
Yeah, if someone has a blank index file to block viewing of an open directory, that code is useless. All is it doing is grabbing the contents and parsing all links to get file names.

01-17-2008, 04:09 PM
Well this is actually what I need. There is no index file to block displaying the content (it's my partner website and I need to list his files, so he did it that way).

Now I will use something like this to 'copy' the file from his server to my server:

$content = file_get_contents('http://www.remotesite.com/folder/thefile.csv');
$dir = "myserver/csv/";
$fp = fopen($dir.'thefile.csv', 'w');
fwrite($fp, $content);

So I managed to do what I need... List files from my partner's server - one click copy of the file I want to my server.

I just wanted to know if there was a better way to achieve this but since it's working fine, that should be ok for me.

Thanks guys!