Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14169
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Subfolder in script |
| Date | 2014-08-12 16:27 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87ppg5sp7p.fsf@bsb.me.uk> (permalink) |
| References | <4a738670-21a1-4de4-9ed9-d9db7d19138c@googlegroups.com> |
Danjel Jungersen <danjel@jungersen.dk> writes:
> 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';"
You mean that you want to list the files in sub2. You don't want to
change $search_dir to "/sub2" because that's the name of another
directory altogether!
> 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.
In a perfect world, programming would not be about luck, but it is. You
had the bad luck come across some code that could not be easily altered.
Step one is to set $search_dir = "sub2";. But this will cause the array
to be populated with the names of the files in sub2, but they are not
know by that name to code whose current working directory in sub1. For
example, sub2 may have a file "doc.pdf", but evaluating
filesize("doc.pdf") will get you nowhere. The file's name (from here)
is "sub2/doc.pdf".
Quick fix:
while ($fname = readdir($dp)) {
$item = "$search_dir/$fname";
/* code as before */
}
> Or a better solution?
Blimey! Too many to name, but I'd consider using the glob function
first of all. glob("$search_dir/*") will give you the names properly
prefixed so it is simpler to change where files are to be found. Also
you will be able to say things like glob("$search_dir/*.pdf") which may
well be useful later.
Another thing would be to use the stat function and to store the result
array directly, rather than picking out just those fields that you need
(which is probably what filesize and filemtime are doing behind your
back). Unless there are a very large number of files, it just won't be
worth doing anything else:
$file_list = array_map(function ($fn) {
$s = stat($fn); $s['name'] = $fn; return $s;
}, glob("$search_dir/*"));
usort($file_list, "cmp");
You may want to string the directory name off the front when writing the
list, and you may need to change the anonymous function to being a named
one if you can't rely on a recent version of PHP.
<snip>
--
Ben.
Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread
Subfolder in script Danjel Jungersen <danjel@jungersen.dk> - 2014-08-12 05:38 -0700 Re: Subfolder in script Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2014-08-12 10:08 -0400 Re: Subfolder in script Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-12 14:27 +0000 Re: Subfolder in script Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-08-12 16:27 +0100
csiph-web