PDA

View Full Version : Anybody groovy with PHP? I need a small snippet of code.


Aszurom
01-08-2003, 07:11 PM
While I've been teaching myself PHP lately, I have not reached the level of familiarity where I can pull this one off... I'm sure it's really simple to anyone who already knows which commands accomplish it tho - it can't be that hard, just elusive in the O'Reiley book.

I need an example of how to do this:

A webpage is passed a filename with directory path in the URL. After I parse the filepath to seperate the file's name from the path, I want to take that path and read the contents of that directory into an array.

Is there some built-in function that accomplishes this, or do I have to open the directory and use a loop to fill the array manually?

chet
01-08-2003, 07:29 PM
Check the file command, it can be a file or a url.

http://www.php.net/manual/en/function.file.php


Edited because I skipped the whole directory part.

Just use the opendir function and walk thru the directory, thedocs give an example.
http://www.php.net/manual/en/function.opendir.php


Chet

Matthew Gallant
01-08-2003, 08:19 PM
Don't forget that the directory you want to read has to allow execute permission to the public.

Aszurom
01-08-2003, 09:38 PM
Thanks... that gets me a lot closer to what I want to do.

The FILE command seems to read the contents of a file into an array, though. What I want to do is get all the filenames in the directory into an array. Thus, if I have a directory full of images, I want to read all the image filenames into an array and then be able to work with them. I'm trying to come up with a homebrew screenshot gallery of sorts.

I'm thinking of using a thumbnail as a link that calls the gallery php page and passes the filename - then the page finds all similarly named files and after displaying the requested image, has a table of the thumbnails of related images.

chet
01-08-2003, 09:46 PM
Then you don't need to read the files in - you just need the file names and then build the img tags on the fly. Making php process the images would add extra overhead.

Chet

Matthew Gallant
01-08-2003, 09:55 PM
This is something I wrote a while ago for here:
http://truemeaningoflife.com/foodbunny/kaoani/kaoani.php
It's not the greatest directory browser ever (I had to put a copy of the script into every sub-directory) but it should help you do what you want to do.

<table align=center>
<tr>
<?php

$dir = opendir('.');
$flag=0;
while ( ($file = readdir($dir)) ) {
if ($file != "." && $file != "..") {
if ( !($flag % 5) )
echo("</tr><tr>");
if ( is_dir($file) ) {
$linkstring = $file . "/kaoani.php";
$fileimage = "folder.gif";
}
elseif ( substr($file, -3) == "gif" ) {
$linkstring = $file;
$fileimage = $file;
}
else {
$linkstring = "";
$fileimage = "";
}
if ($linkstring) {
echo("<td align=center nowrap><a href=\"" . $linkstring . "\"><img src=\"" . $fileimage . "\" border=0>
" . $file . "</td>\n");
$flag++;
}
}
}

closedir($dir);
?>
</tr>
</table>

Aszurom
01-09-2003, 02:25 PM
Yeah, I was able to find it QUITE clearly in the WROX book - once I knew what I was looking for. I'm going to experiment with how I want this thing to behave, because I'm not quite sure just yet.

Option 1 - Include a function that renders the link and image tags when passed an image name from within the article. Would save a lot of typing if I just had <? LinkImage("/gallery/screentshot01.jpg") ?> instead of having to write the actual HTML over and over again.

Option 2 - Sniff a lot of glue and then try to write an opensource module for PostNuke that has built-in gallery administration functions.

Option 1 kinda rules out Option 2 according to their standards of module development - since I'd have to hack the core code to get the function included I think. I'll research it... maybe save some other people some pain.