Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subfolder in script.

9 views
Skip to first unread message

dan...@jungersen.dk

unread,
Aug 12, 2014, 8:33:37 AM8/12/14
to
Hi there.

I have found this online:
// this is used by usort()
function cmp($a, $b) {
if ($a['mtime'] == $b['mtime']) {
return 0;
}
return ($a['mtime'] < $b['mtime']) ? -1 : 1;
}
// read dir and save name,size,date in array
$file_list = array();
$search_dir = '.';
$dp = opendir($search_dir);
$file_list = array();
while ($item = readdir($dp)) {
if ((is_file($item)) AND (substr($item, 0, 1) != '.')) {
$file_list[] = array('name' => $item, 'size' => filesize($item), 'mtime' => filemtime($item));
}
}
// Sort $file_list
usort($file_list, "cmp");
// Create a table header.
print '<hr /><br />
<table cellpadding="2" cellspacing="2" align="left">
<tr>
<td><b>File Name</b></td>
<td><b>File Size</b></td>
<td><b>Last Modified</b></td>
</tr>';
foreach($file_list as $one_file) {
// Print the information.
print "<tr>
<td><a href=\"" . $one_file['name'] . "\">" . $one_file['name'] . "</a></td>
<td>" . $one_file['size'] . " bytes</td>
<td>" . date('F j, Y', $one_file['mtime']) . "</td>
</tr>\n";
}
print '</table>'; // Close the HTML table.
closedir($dp); // Close the directory.

The .php file is stored on domain.tld/sub1

The files are at domain.tld/sub1/sub2

I want to change "$search_dir = '.';" to "$search_dir = '/sub2';"

But I cannot get it to work.
I have tried all the combinationa of sub1 / sub2, with and without "." ending and starting "/".
But no luck.

Anyone?

Or a better solution?

I need to create a clickable list of files.
The files are pdf files, and the links can be generated from filenames.

:-)
Danjel

Frank Damgaard

unread,
Aug 12, 2014, 11:45:57 AM8/12/14
to
Den 12-08-2014 14:33, dan...@jungersen.dk skrev:
> Hi there.
>
> I have found this online:
.....

> $file_list = array();
> $search_dir = '.';
> $dp = opendir($search_dir);

....snip....

> if ((is_file($item)) AND (substr($item, 0, 1) != '.')) {

filename $item is relative to current working directory what is not
the intention. $search_dir must be added:

if (is_file($search_dir .'/'.$item) AND (substr($item, 0, 1) != '.')) ) {


> The .php file is stored on domain.tld/sub1
>
> The files are at domain.tld/sub1/sub2
>
> I want to change "$search_dir = '.';" to "$search_dir = '/sub2';"
>
> But I cannot get it to work.

relative paths works , so ok for "sub2" if sub2 exists.
Directory is sorted with ascending mtime.

remember "/sub2" is absolute path on server !
not local-realtive to your local domain on server.
so maybe it should be something like: /var/www/domain-tld/sub1

Danjel Jungersen

unread,
Aug 13, 2014, 6:31:56 AM8/13/14
to

if (is_file($search_dir .'/'.$item) AND (substr($item, 0, 1) != '.')) ) {

Just needed "if ((is_..."

And everything works, perfect, thanks....

:-)
Danjel

Frank Damgaard

unread,
Aug 14, 2014, 1:45:36 AM8/14/14
to
remember other file operations on $item:

...... filesize($item), 'mtime' => filemtime($item)).....


0 new messages