Jump to content

read txt file description on gallery page


busnut

Recommended Posts

G'day all, i've got this script that I found on the net some time back but not entirely sure where, however it works well except I want to add a feature so it shows the image details from a txt file. Here is the gallery script:

 

 

<?phpfunction getDirectory( $path = '.', $level = 0 ){     $ignore = array( 'cgi-bin', '.', '..' );     $dh = @opendir( $path );     while( false !== ( $file = readdir( $dh ) ) ){         if( !in_array( $file, $ignore ) ){             if( is_dir( "$path/$file" ) ){ 			$dir = $path . $file;                 echo "<h2>$file</h2><p>";                 $dh1 = @opendir( $dir );                 while( false !== ( $image = readdir( $dh1 ) ) ){                     if( !in_array( $image, $ignore ) ){                         if( !is_dir( "$dir/$image" ) ){       if (preg_match("/.jpg/", $image)) {		echo "<a href=\"$dir/$image\" rel=\"lightbox\"><img src=\"gallery-thumbnail.php?file=$file/$image\" border=2 style='border-color: #981C1E'></a>\n"; }                        }                     }                 } echo "</p>";                $level++;             }         }     }     closedir( $dh ); } getDirectory('gallery/'); ?>

 

 

Now, what i'm wanting is to read a txt file either in the root or 'gallery' directory, where it has the file name, title, description, photographer. (something like filename.jpg|title of image|description about image|J King).

so when the image is displayed, a script reads the text file and will show the image info beside it.

 

Now i've got this bit of snippet from an old script I used once, but can't seem to get it to work anywhere, so any help is greatly appreciated.

 

$text = "gallery.txt";$fh=fopen($text, "r"); while(!feof($fh)) {   $temp = explode("|", $line);   $title[$temp[0]] = $temp[1];  $description[$temp[0]] = $temp[2];  $photographer[$temp[0]] = $temp[3];  $line=fgets($fh);   unset($temp); } 

 

Link to comment
Share on other sites

Perhaps something like...

 

$gallery = "gallery.txt";$lines = file($gallery);$num_pics = count($lines);$i = 0;while($i<$num_pics) {$temp_array = explode("|",$lines[$i]);$image=$temp_array[0];$title = $temp_array[1];$description = $temp_array[2];$photographer = $temp_array[3];?><div>	<IMG src="<?PHP echo $image; ?>" align="left"> 	Title: <?PHP echo $title; ?>"<br>	Photographer: <?PHP echo $photographer; ?>"<br>	<?PHP echo $description; ?></div><?PHP$i ++;}

 

Link to comment
Share on other sites

here, $line isn't set so explode won't do anything:

 

while(!feof($fh)) { 
  $temp = explode("|", $line); // $line isn't set, at least the first time through.

 

I expect that explode would fail, throwing an error. do you have error_reporting turned off?

No error, just won't display any details.

Perhaps something like...

$gallery = "gallery.txt";
$lines = file($gallery);
$num_pics = count($lines);
$i = 0;
while($i<$num_pics) {
$temp_array = explode("|",$lines[$i]);
$image=$temp_array[0];
$title = $temp_array[1];
$description = $temp_array[2];
$photographer = $temp_array[3];
?>
<div>
	<IMG src="<?PHP echo $image; ?>" align="left"> 
	Title: <?PHP echo $title; ?>"<br>
	Photographer: <?PHP echo $photographer; ?>"<br>
	<?PHP echo $description; ?>
</div>
<?PHP
$i ++;
}

One thing I forgot to mention is that the gallery script is hosted in the public_html folder, and the script looks for images in sub folders of the gallery folder (ie, gallery/topic1, gallery/topic2 etc etc)

 

I tried your example, but because of where the images are located, I just get a square cross box and shows the file name.

Link to comment
Share on other sites

does each image folder have its own gallery text file?

 

Is there a reason you aren't using a database for this?

 

There is currently only one gallery text file located in the public_html directory, however I can have a seperate one for each category if required if that would help.

 

As for not having a database, i'm just one of several people in the non-profit organisation that i'm doing the website for that can upload images and information, and putting it politely, a couple of the people who are able to upload images, i'm trying to make it as basic as possible for them as they really have no idea on what to do and I don't want to confuse them with extra steps on how to go about updating information (its taken 2 years for one member to understand how to retrieve the member listing from the database :()

Link to comment
Share on other sites

No problem. Understood.  Eleemosynary activites are to be lauded.

 

One text file is fine. However, it would simplify matters if the path to the image was included as a field in the text file.

 

 

I did a couple of different things thinking of that already, one was say "gallery/topic1/bla.jpg" and still didn't work :(

I've tried numerous things, generally don't get error messages, but look at the page source and there isn't even anything there trying to display the text.

At one stage, i did get the word 'NULL' being displayed for the information.

Link to comment
Share on other sites

Discovered 1 issue that i've resolved. See the php script below that is now working - however I now need it to sort between each of the subfolders (eg topic1, topic2, topic 3 etc). I believe i'll have to add another field to the text file which won't be a problem. And the other problem i've came acoss is if an image exist but no information, the picture isn't displaying :(

 

<?php

$gallery = "gallery.txt";
$lines = file($gallery);
$num_pics = count($lines);
$i = 0;
while($i<$num_pics) {
$temp_array = explode("|",$lines[$i]);
$image=$temp_array[0];
$title = $temp_array[1];
$description = $temp_array[2];
$photographer = $temp_array[3];

?>

	<a href="<?PHP echo $image; ?>" rel="lightbox" title="<?PHP echo $title; ?><br><?PHP echo $description; ?><br>© <?PHP echo $photographer; ?>">
	<img src="gallery-thumbnail.php?file=<?PHP echo $image; ?>"></a>

<?PHP
$i ++;
}
?>

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.