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
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/
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
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194179
Share on other sites

Note to TRI0N: Thats pretty inefficient code, allocating everything in memory and then reading it from memory... this means your reading the data twice. Perhaps just display it as it comes?

 

Also single quotes are faster than double quotes :)  (' & ' compared with " & ").

Link to comment
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194186
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
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194195
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
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194199
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
https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194202
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.