SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passthru(ls -alr) and opendir - at the same time

    I'm able to print a directory with opendir, but currently the files are in random order.

    My goal is to determine a way to use passthru (or some similar function... exex or system maybe) to sort the directory prior to listing it on the screen. I want to use opendir so I can make a link to the files (otherwise a simple <pre></pre> output would do nicely).

    Am I barking up the wrong tree, or can this be accomplished?

    If so, where do I put the passthrough command in the context of this:

    Code:
    	$path = '/var/www/html/files/';
    	$dir_handle = opendir($path) or die("Unable to open $path");
    	while ($file = readdir($dir_handle)) {
    		$filedate = filectime("$path$file"); 
    		$filedate = date("m/d/Y G:i:s", $filedate); 
    		$file_size = filesize("$path$file"); 
    		echo "<a href='http:///files/$file' target=new>$file</a><br>";
    	}
    Thanks!
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you want to sort them? By name? Or by date modified or how? You should probably stick them into an array and then sort them that way, it will be more secure? Is the sort method going to change very much? Or is just one sort method?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I came up with for you. Kind of crude but I think you'll get the idea. It sorts in asc and rev order on any field in the row.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <?
    $d 
    dir(".");
    $sorter = ($sorter) ? $sorter 'filename';
    $sortorder = ($sortorder) ? $sortorder 'asc';

    $i 0;
    while(
    $f $d->read()) {
        if((
    $f != '.') && ($f != '..') && !is_dir($f)) {
            switch(
    $sorter) {        
                case 
    'filesize':
                
    $files[] = array("filesize" => filesize($f),
                                 
    "filedate" => date("m/d/Y G:i:s"filectime($f)),
                                 
    "filename" => $f
                                
    );
                break;
                case 
    'filedate':
                
    $files[] = array("filedate" => date("m/d/Y G:i:s"filectime($f)),
                                 
    "filename" => $f,
                                 
    "filesize" => filesize($f)
                                );
                break;
                default:
                
    $files[] = array("filename" => $f,
                                 
    "filedate" => date("m/d/Y G:i:s"filectime($f)),
                                 
    "filesize" => filesize($f)
                                );
                break;
                }

            }
        }

    if(
    $sortorder == 'asc') {
        
    sort($files);
        }
    else {
        
    rsort($files);
        }
    $d->close();


    ?>
    <table width=600 border=1>
        <tr><td><a href="<?=$PHP_SELF?>?sorter=filename&sortorder=asc"><strong>File Name</strong></a> <a href="<?=$PHP_SELF?>?sorter=filename&sortorder=desc">(rev)</a></td><td><a href="<?=$PHP_SELF?>?sorter=filesize&sortorder=asc"><strong>File Size</strong></a> <a href="<?=$PHP_SELF?>?sorter=filesize&sortorder=desc">(rev)</a></td><td><a href="<?=$PHP_SELF?>?sorter=filedate&sortorder=asc"><strong>Last Modified</strong></a> <a href="<?=$PHP_SELF?>?sorter=filedate&sortorder=desc">(rev)</a></td></tr>

    <?
    foreach($files as $key => $val) {
    ?>
        <tr><td><?=$files[$key][filename]?></td><td><?=$files[$key][filesize]?></td><td><?=$files[$key][filedate]?></td></tr>
    <?
        
    }
    ?>
    </table>

    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I take it there isn't a way to do this without using arrays? The directory I will be listing sometimes has over 500 files. I'm afraid that by loading the files in an array I will slow the page down quite a bit.

    This method works great on a smaller directories, though (already wrote something similar to your example).

    Thanks for any extra info...
    - Mike
    http://www.georgiaoffroad.com

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even with 500 elements to the array, I think it will be okay, okay I suppose you could do it with a system call, but I think its going to be a security hazard.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •