[UPHPU] simple??? php function (display an index)

Caleb Call caleb at macjunk.org
Fri Jul 21 22:51:23 MDT 2006


Thanks for the info (I finally got around to being able to look-up  
some of this stuff) and I really like the glob() function, I think  
I'll use that...couldn't that be used (partially) to do the thumbnail  
thing you suggested as well?


On Jul 19, 2006, at 10:41 PM, Mac Newbold wrote:

> Today at 9:59pm, Caleb Call said:
>
>> thanks, that got me going, I found some examples.  (This php stuff  
>> is all new but great to finally get playing with it).  This is  
>> what I have so far, can anyone suggest a better way to do any of it?
>
>> $drop = array("www.", "yourwebsite", ".com/");
>> # here you drop the url, ex: www.yourwebsite.com/
>> $pickup = array("", "", "");
>>
>> $folder = str_replace($drop, $pickup, $DirNow);
>
> I was going to ask why you did an array of replacements if  
> replacing the URL was what you were after, but then realized that  
> this way it can replace individual parts where it finds them, which  
> can be good or bad depending on your goal. Here it seems good. Neat  
> trick I'll have to remember.
>
>> if ($handle = opendir('.')) {
>>  while (false !== ($file = readdir($handle))) {
>
> One option that is even easier IMHO than opendir and readdir is the  
> glob() function. foreach (glob(*) as $file) would seem to do  
> exactly what you need.
>
>>      if ($file != "." && $file != ".." && $file != "index.php" &&  
>> $file != "index.html") {
>
> If you want, this can be streamlined:
>
> if (!in_array($file,array(".","..","index.php","index.html"))) {
>
> Of course, you can also put the array outside the call and save it  
> in a variable too.
>
>>      	   $file_size = filesize($file);
>>      	   $file_size = round(($file_size/1024),0);
>>          echo "<a href=$file <br>$file\t\t\t</a>$file_size KB<br>\n";
>>      }
>>  }
>>  closedir($handle);
>
> With glob there's no closedir either.
>
>> is there a way I can display KB on smaller files and MB on larger  
>> files?  I would assume a loop, something like this?
>>
>> if ($file_size >= 1024) {
>> 			echo "($file_size/1024) MB";
>>          } else {
>>          	echo "($file_size) KB";
>>          }
>>
>> it's not working so I know there is something wrong with it, but  
>> what???
>
> It's probably printing things like "12983/1024 MB" right? The slash  
> and the parentheses will be printed literally inside your double  
> quotes, but this might come closer to what you intended:
>
> echo round($file_size/1024)." MB";
>
> In fact, you could replace your whole echo statement like so:
>
> echo "<a href=$file <br>$file\t\t\t</a>".
>   ($file_size < 1024 ? "$file_size KB" :
>    round($file_size/1024)." MB")."<br>\n";
>
> The ternary operator is kind of like an if statement that returns  
> an expression, so ($cond ? $iftrue : $iffalse) saves you a real if  
> statement and returns the proper string for you so that it can be  
> echo'd. They're extremely handy for setting default values:
>
> $var = (!isset($val) || !$val ? $defaultval : $val);
>
> That works great if $val is something like $_GET['key'] for example.
>
> If you wanted to fancy up your directory index a bit, you can  
> always look for things that are images, for example, and show a  
> thumbnail, or even turn the directory index script into a very  
> simple gallery script you can drop into a directory of images. Or  
> as fungus/Lonnie was suggesting, pass a directory name to it and  
> have it show all the images in the folder. The way you've written  
> it, it can be pretty generic so you can drop it in just about any  
> folder and get an instant directory for it.
>
> With a little bit more work, you could probably turn it into a file  
> browser where you'd only need one copy of the script. An .htaccess  
> file that specifies the script an ErrorDocument or something might  
> be able to help with that.
>
> A simple script like that is also the basis for a pretty list of  
> downloadable files within a larger site. Throw a bit more html/css  
> around it so it matches the rest of the site, or include it from  
> another page, and it's easy to show them a list of files in a  
> directory and let them view or download whatever you want them to.
>
> Thanks,
> Mac
>
> --
> Mac Newbold		MNE - Mac Newbold Enterprises, LLC
> mac at macnewbold.com	http://www.macnewbold.com/



More information about the UPHPU mailing list