Jump to content

perhaps you can help


Maverickb7

Recommended Posts

hello--i'm trying to create a script that will read files from various directories on my site. I've come up with some code that does what I want but I can't figure out how to specific a directory through a variable.

 

foreach (glob("*.txt") as $filename) {
$filesize = filesize($filename);
$filesize = humansize($filesize);
echo $filename." - ".$filesize;
}

 

I've toned it down a bit so its easier to read. But basically at the moment the script only reads the directory its being run from. I want the directory path stored in a variable and included somehow... anyone know?

 

Link to comment
Share on other sites

Well anyways I'm assuing you just want to show text files (*.txt) so here is a something I use to do this..  I also included a commented line so that if you need to show more then just .txt you can and it will hide .php file that might be in that dir.

 

Bonus it shows last modified date and MD5 Checksum.  Have fun!

 

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset="iso-8859-1">
<link rel="stylesheet" type="text/css" href="../global.css">
<title>Greenskeeper.org :: Event Tracker</title>
<style fprolloverstyle>A:hover {color: #FF0000}
</style>
</head>

<?php
$path = "./"; // path to the directory to read ( ./ reads the dir this file is in)
if ($handle = opendir($path)) {
  while (false !== ($file = readdir($handle))) {
//    if($file != "." && $file != ".." && strrchr($file, ".") != ".php") {
   if($file != "." && $file != ".." && strrchr($file, ".") == ".txt") {
       if(!is_dir($file)){
           $item[] = $file ;
           $file_size = round(filesize($file)/1024,2) ;
           if($file_size >= 1024){
              $filesize[] = "File Size: ".round($file_size/1024,2) ;
              $file_xb[] = " MB" ;
              } else {
              $filesize[] = "File Size: ".round(filesize($file)/1024,2) ;
              $file_xb[] = " KB" ;
              }
           $filectime[] = "Last Modified: ".date("d/m/Y G:i:s",filectime($file)) ; // set last changed date
           $md5[] = md5_file($file) ;
	$md5tag[] = "<font color='#FF0000'>MD5</font>: " ;
           }
      }
  }
  closedir($handle);
}

$total_items = count($item);
$max_items = ceil($total_items / 3); // items per <td>
$start = 0;
$end = $max_items
//generate the table
?>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
 <tr><p>
   <?php
   for($i=0; $i<3; $i++){
       if($i != 0){
           $start = $start + $max_items;
           $end   = $end + $max_items;
       }
       echo "<td><p>";
       for($x = $start; $x < $end; $x++){
           // display the item
           echo '<a href = "'.$path.$item[$x] .'">' ;
           echo $item[$x]."</a><br>" ;
	echo $filesize[$x].$file_xb[$x]."<br>" ;
           echo $filectime[$x]."<br>" ;
           echo $md5tag[$x].$md5[$x]."<br><br>" ;
       }
   echo "</p></td>";
   }
   ?>
 </p></tr>
</table>

Link to comment
Share on other sites

I incluced that it's the 2nd line down of the PHP Code...

 

$path...  So from root lets say you want to look at the files in /docs

 

$path = "./docs"; // path to the directory to read ( ./ reads the dir this file is in)

 

Simple stuff...

Link to comment
Share on other sites

My code will work but it being so called not effcient even after it needs to looks at efficiency for a single block 1KB differnence then well I'm sorry..

 

The code works fine for small directories need to provide a simple file browser example. 

 

However I withdraw... Hope you get it fixed.

Link to comment
Share on other sites

I'm not saying anything about your code. I didn't want something re-written for me. I was trying to find a way to modify the code I had created above to do what I was asking. I just need away to tell that code to look in X directory using a variable. I'm sure its only like 2 seconds of work for someone who knows what they are doing. I'm still new to all of this so any help would be appreciated.

Link to comment
Share on other sites

foreach (glob("/var/www/html/*.php") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
}

 

Did you even try putting in a directory?  ::) ::)

 

This lists all .php file in the directory /var/www/html/ (which is where my web site lives on my Red Hat box) I assume this would also work with : glob('C:\*.txt');

 

Hope that helps  :)

 

monk.e.boy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.