Maverickb7 Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 $path = "./"; // path to the directory to read ( ./ reads the dir this file is in) if ($handle = opendir($path)) { Not the complete script but an Idea that should help you figure out how to get opendir($path) into yours. Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194156 Share on other sites More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 isn't glob and opendir two different methods? how would I include $path into the code i've already made? Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194158 Share on other sites More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 Do you only want to display .txt files in it? Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194167 Share on other sites More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 yes. Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194176 Share on other sites More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 Thanks, I'll take a look at your code. But do you know of anyway I could edit the code I created to do this? I just need some way to tell the code what directory to look in using a variable. Could you help me? Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194181 Share on other sites More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 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 https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194182 Share on other sites More sharing options...
heckenschutze Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 Your welcome to contibute to a fix if my code is not up to par. Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194191 Share on other sites More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 I just need some way to tell the code what directory to look in using a variable. Could you help me? Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194194 Share on other sites More sharing options...
TRI0N Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 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 More sharing options...
monk.e.boy Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 Yes, that helped me monk.e.boy. Thanks. For some reason I tried that before and it didn't want to work for me but this time around it worked so thanks. Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194455 Share on other sites More sharing options...
Maverickb7 Posted February 26, 2007 Author Share Posted February 26, 2007 ok, one more question. =( does glob() have to use a path or can it use a URL as well? Link to comment https://forums.phpfreaks.com/topic/40130-perhaps-you-can-help/#findComment-194504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.