Click to See Complete Forum and Search --> : opendir question


comptech520
10-26-2007, 08:59 AM
I am trying to have an index.php file in the home directory looking in to the directory 1234. When the links appear they are directed to the mydomain.com directory. How can I direct them to the mydomain.com/1234 directory.

<?
// Define the full path to the folder whose contents you want to list
$path = "/var/www/vhosts/mydomain.com/httpdocs/1234";
// Open the directory
$dir_handle = @opendir($path) or die("Error opening $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "1234/" ) { continue; }

echo "<a href=\"$file\">$file</a><br />";
}
// Close
closedir($dir_handle);
?>

bokeh
10-26-2007, 09:46 AM
Maybe use a relative path:<?
// Define the relative path to the folder whose contents you want to list
$path = "./1234";
// Open the directory
$dir_handle = @opendir($path) or die("Error opening $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "1234/" ) { continue; }

echo "<a href='{$path}{$file}'>{$file}</a><br />";
}
// Close
closedir($dir_handle);
?>

comptech520
10-26-2007, 11:01 AM
That worked, Thanks!

Is there a way that I can sort the files ascending?

NightShift58
10-26-2007, 05:09 PM
The easiest way I can think of is to use glob() instead of opendir().

Once you have the directory into the array returned by glob(), use asort() to sort it.

bokeh
10-27-2007, 09:34 AM
That worked, Thanks!

Is there a way that I can sort the files ascending?It depends what you mean by sort. If you are talking about by filename you could use Nightshift's suggestion, if not are you talking about by date, modified or what?

comptech520
10-27-2007, 03:27 PM
I looked on php.net and other resources, I still couldn't find something that with glob and asort.

Could you kindly give me an example?

bokeh
10-27-2007, 05:52 PM
Here's one way (not tested):<?php

$path = "./1234";
chdir($path);
$files = glob('*.*');
sort($files);
foreach($files as $file)
{
echo "<a href='{$path}/{$file}'>{$file}</a><br />";
}
?>

NightShift58
10-28-2007, 06:25 AM
<?php
$path = "./1234";
$files = glob("$path/*");
sort($files);
foreach($files as $file)
{
echo "<a href='$path/" . basename($file) . "'>" . basename($file) . "</a><br />";
}
?>
Don't use "*.*" with glob(), as it will then only return file and directory names with "name.ext" and none with just "name.".

glob() also returns the file name with file system path so it will need some converting to function as an HTTP path.

bokeh
10-28-2007, 07:54 AM
glob() also returns the file name with file system path so it will need some converting to function as an HTTP path.Nightshift, look at my post. Without a path glob() works on the CWD. So all you need do is chdir() to the directory you want to scan. When working on the CWD glob() does not return the path, only the filenames.

And to the OP, as Nightshift points out, if you want the complete directory contents (from the CWD) use glob('*') or if you want only the conventionally name file (no directories, .ht's or bare filenames) use glob('*.*') or whatever pattern suits your need.

By the way, if you are using PHP5 consider using scandir() as this has sorting built in and does everything in one easily digestible step.