List files in folder?
Moderator: General Moderators
List files in folder?
Ok, I just got a fileserver started with mIRC... on top of THAT, I just added php support. I am quite happy with myself. ^_^
So anyway, how can I echo all the files in a folder? (with them set inside anchors). Also, I'd like to add an icon next to certain types. (piece of paper next to txt files, etc).
So what's a simple way to do this?
(*crosses his fingers on 'simple'*)
So anyway, how can I echo all the files in a folder? (with them set inside anchors). Also, I'd like to add an icon next to certain types. (piece of paper next to txt files, etc).
So what's a simple way to do this?
(*crosses his fingers on 'simple'*)
Code: Select all
<?PHP
$whatsinthedir=opendir ('.');
while ($file = readdir ($whatsinthedir)) {
echo "$file <br>";
}
closedir($whatsinthedir);
?>you can also try, i wrote it this weekend... http://home.mysth.be/~timvw/browser/browser.php
source @ http://home.mysth.be/~timvw/programming/php/browser.zip
source @ http://home.mysth.be/~timvw/programming/php/browser.zip
well, I've played around with it, but for some reason it keeps wanting to load something or whatever sometimes.... like it kinda freezes. What did I do wrong in the parts I changed?:
Code: Select all
if (isset($_GET['action']))
{
switch($_GET['action'])
{
case 'cd':
if (substr_count($_GET['path'], "..") == '0' || $_SESSION['cwd'] != 'C:\BeZkript_me\webfiles\files') {
@chdir($_GET['path']);
}
$_SESSION['cwd'] = getcwd();
break;
case 'get':
header('Content-length: ' . filesize($_GET['path']));
header('Content-type: application/octetstream');
header('Content-Disposition: attachment; filename=' . $_GET['path']);
readfile($_GET['path']);
exit();
default:
}
}Code: Select all
function output_all_files($path)
{
echo "<div><table>";
if ($path != 'C:\BeZkript_me\webfiles\files' && $path != '.') {
echo "<tr><td width='30'><img src='img/back.png' /></td><td width='300'><a href='?action=cd&path=..'>Parent Directory</a></td><td width='100'> </td><td width='180'> </td></tr>";
echo "<tr><td colspan='4'><hr width='100%' /></td></tr>";
}
//...Code: Select all
<div>Current Location:
<?php
$currentpath = $_SESSION['cwd'];
if ($currentpath == 'C:\BeZkript_me\webfiles\files' || $currentpath == '.') {
echo '/';
}
else {
$currentpath = str_replace("C:\BeZkript_me\webfiles\files", "", $currentpath);
echo str_replace("", "/", $currentpath);
}
?>
</div>I assume you want to allow people only to a given part of the filesystem?
this sample limits them to /home/timvw/src/home.mysth.be/webroot
this sample limits them to /home/timvw/src/home.mysth.be/webroot
Code: Select all
// handle actions
if (isset($_GET['action']))
{
switch($_GET['action'])
{
... i cutted some code away
}
}
// this was added
if (!preg_match("/^\/home\/timvw\/src\/home.mysth.be\/webroot/", getcwd()))
{
echo "<b>This demo allows you only to browse in /home/timvw/src/home.mysth.be/webroot</b>";
@chdir('/home/timvw/src/home.mysth.be/webroot');
$_SESSION['cwd'] = getcwd();
}alrighty! thanks. Now how can I make it work for that folder AND all subfolders?
btw, got it to change the windows address to linux style w/ the main dir as home ^_^ :
btw, got it to change the windows address to linux style w/ the main dir as home ^_^ :
Code: Select all
$location = str_replace('C:\\BeZkript_me\\webfiles', '', $_SESSION['cwd']);
$location = str_replace('\\files', '/', $location);
echo str_replace('\'', '/', $location);it already works for subfolders, because i only see if the start of the cwd (current working directory) starts with /home/timvw/whateverMini-Me wrote:alrighty! thanks. Now how can I make it work for that folder AND all subfolders?
if i only wanted to allow a folder i would use ^/whatever/path$ as regular expression.
>.> oh. Well, I guess that means I'm doing something wrong. ^^; I probably just put the path in wrong... ^^;
Here's what I have:
Here's what I have:
Code: Select all
if (!preg_match("/C:\/BeZkript_me\/webfiles\/files/", getcwd()))
{
@chdir('C:\\BeZkript_me\\webfiles\\files');
$_SESSION['cwd'] = getcwd();
}meaby you could try case insensitive preg_match?
btw, i've changed the code, made it more flexible... currently output_html function accepts an array with the files (and info on them).
this way i can first retrieve the files and then generate output. the nice thing is that i can build such an array from a ftp-listing etc... here is a little sample i use to browse a different/remote filesystem:
btw, i've changed the code, made it more flexible... currently output_html function accepts an array with the files (and info on them).
this way i can first retrieve the files and then generate output. the nice thing is that i can build such an array from a ftp-listing etc... here is a little sample i use to browse a different/remote filesystem:
Code: Select all
<?php
error_reporting(E_ALL);
function xml_list_files($path)
{
$xml = '<folder>';
if (($files = glob("$path/*")) !== false)
{
foreach ($files as $file)
{
$name = htmlentities(basename($file));
$ctime = htmlentities(date('Y-m-d H:i:s', filemtime($file)));
$real = htmlentities(realpath($file));
// handle every file, execpt . and ..
if ($file != '.' && $file != '..')
{
if (is_link($file))
{
$source = htmlentities(readlink($file));
$xml .= "<file type='link' ctime='$ctime' real='$real' src='$source'>$name</file>";
}
elseif (is_dir($file))
{
$xml .= "<file type='directory' ctime='$ctime' real='$real'>$name</file>";
}
else
{
$size = htmlentities(filesize($file));
$xml .= "<file type='file' ctime='$ctime' real='$real' size='$size'>$name</file>";
}
}
}
}
$xml .= '</folder>';
return $xml;
}
if (isset($_GET['path']))
{
$path = $_GET['path'];
}
else
{
$path = '.';
}
echo xml_list_files($path);
?>