digster
digster

Reputation: 402

Create clickable links of local files using exec() command?

I am a beginner to PHP so please forgive any ignorance...

I am using the exec() command as following to get the list of files in my media directory..

<?php // exec.php
$cmd = "dir";   // Windows
exec(escapeshellcmd($cmd), $output, $status);
if ($status) echo "Exec command failed";
else
{
echo "<pre>";
foreach($output as $line) echo "<a href='$line'>$line</a> \n";
}
?>

The problem is it gives the list of files along with the various timestamps of the filenames-

 Volume in drive F is Movies 
 Volume Serial Number is 172B-1DE0 
 06/17/2011  01:11 AM             6,318 bck.gif

Hence, here it creates clickable link to each line for the output which needless to say does not work.

What I want is that it will only create clickable links for the filenames and not the extra meta information, which the user can then click to launch his native program like this-

video1.mpg
video2.mpg
bck.gif 

Upvotes: 1

Views: 839

Answers (2)

Jacco
Jacco

Reputation: 23799

There is no need to use exec(); to list files in the directory, PHP has many build in functions for dealing with the file system:

From the readdir() manual page:

<?php
if ($dirHandle = opendir('.')) {
    while (false !== ($nodeHandle = readdir($dirHandle ))) {
        if ($nodeHandle == '.' || $nodeHandle == '..') {
            continue;
        }
        echo "$nodeHandle \n";
    }
    closedir($dirHandle);
}
?>

Upvotes: 1

GordonM
GordonM

Reputation: 31750

You're far better off using PHP's directory manipulation functions instead. The scandir() function should be of particular interest to you.

Don't forget that the scandir listing will include . and .. ao you'll need to remove them from the results set unless you plan to use them for navigation.

Upvotes: 2

Related Questions