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
You're far better off using PHP's directory manipulation functions instead. The scandir() function should be of particular interest to you.
- http://uk.php.net/manual/en/book.dir.php
- http://uk.php.net/manual/en/function.scandir.php
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.
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);
}
?>
精彩评论