read multiple text files from directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

read multiple text files from directory

Post by kyleiwaniec »

I am trying to create a thumbnails and captions "gallery" (no large images like in a traditional gallery, just the thumbs/captions) which draws the thumbnails and the captions from directories on the server.

The images show up as expected.

I would like to read a number of text files from another directory (I do not want to hardcode the file names) next to the images.

this is what I have:

Code: Select all

  $files = glob("images/*.*");  
      for ($i=1; $i<count($files); $i++) {  
         $num = $files[$i];  
   
      $theData = file_get_contents("captions/",'r'); 

    print $num."<br />"; 
    echo '<li><img src="'.$num.'" alt="random image"><div id="caption">'.$theData.'</div></li>'."&nbsp;&nbsp;";  
    }  
     
    $fh = fopen("captions/",'r'); 

But this gives me an error:
Warning: fopen() expects parameter 1 to be string, array given in ... line 28
Warning: fgets(): supplied argument is not a valid stream resource in .... line 29
Thanks,
Kyle
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

That is not all of your code and that fopen() is not from line 28.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

I'm sorry about the line 28. My mistake.

Here is the entire php page:

Code: Select all


<html>
<head>
<title>Demo</title>


</head>
<body>

<h1>Sequential Images from Directory</h1>


<!--  output  images in directory listing  -->
<ul>
  <?php 
  
  $files = glob("images/*.*"); 
 	 for ($i=1; $i<count($files); $i++) { 
 		$num = $files[$i]; 
  
  	
	print $num."<br />";
	echo '<li><img src="'.$num.'" alt="random image"><div id="caption">sdcsdc</div></li>'."&nbsp;&nbsp;"; } 
	
	$files = glob("captions/*.*");
	
	 	 for ($i=1; $i<count($files); $i++) { 

			$fh = fopen($files, "r");
				while ( $line = fgets($fh, 1000) ) {
					
		print $line;
}
		 }
	
	?>
</ul>

</body>
</html>


I hope this helps.

Thanks for the quick response,
Kyle
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

Please pardon my inconsistent code. Something went awry on my end. The last code posted is my most current code.
I hope I haven't put anybody off.

Thanks,
Kyle
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

#1 You're reusing $i and $files in the nested loop, so it will reset it for the outer loop. Change it to $j and $captions maybe. #2 You did it correctly for the images, so do this for the captions:

Code: Select all

$fh = fopen($images[$j], "r");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

AbraCadaver wrote:#1 You're reusing $i and $files in the nested loop, so it will reset it for the outer loop. Change it to $j and $captions maybe. #2 You did it correctly for the images, so do this for the captions:

Code: Select all

$fh = fopen($images[$j], "r");
Never mind the $i and $files part, I didn't see they are not nested due to the braces and indentation:

Code: Select all

$fh = fopen($files[$i], "r");
Also, this may be easier:

Code: Select all

foreach (glob("captions/*.*") as $file) {
	$lines = file($file);
	echo implode('<br>', $lines);	
}
Or:

Code: Select all

foreach (glob("captions/*.*") as $file) {
	echo file_get_contents($file);
}
Depending upon what you want.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

Thank you so much. It works, I just have one more quandry. I would like the captions to appear relative to the images.

Any ideas?

my php code currently:

Code: Select all

 <?php 
  
  $files = glob("images/*.*"); 
 	 for ($i=1; $i<count($files); $i++) { 
 		$num = $files[$i]; 
  	
	foreach (glob("captions/*.*") as $file) {
        $lines = file($file);
       echo implode('<br>', $lines);   
}
  	
	//print $num."<br />";
	echo '<li><img src="'.$num.'" alt="random image"><div>something here'</div></li>'."&nbsp;&nbsp;"; } 
	


		 	
	?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

I'm not sure what you mean.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

Right now the text files have been concatenated and appear above each image. This is what it looks like:

http://www.mercercountyrealtors.com/pre ... ential.php

I would like for each image to have its own caption. ex: image1/caption1, image2/caption2, etc...

Does that make any sense?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

kyleiwaniec wrote:Right now the text files have been concatenated and appear above each image. This is what it looks like:

http://www.mercercountyrealtors.com/pre ... ential.php

I would like for each image to have its own caption. ex: image1/caption1, image2/caption2, etc...

Does that make any sense?
Does each caption file only contain 1 caption? If so, how does the image file relate to the caption file, by the number 1,2,3, etc.? If not, then it is very confusing.

This may be a flawed design that needs to be done a different way.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

Yes, each caption file contains one caption.

Right now the images and captions aren't related through their names, but I can make them so (haven't created all the images yet).

I need the images and captions to be fetched from directories, because later on I will need to refer to them from different pages, and galleries. I don't want to have to update the same images on different pages.

I've tried every image gallery on the web (I think), and can't find a solution. Any ideas?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: read multiple text files from directory

Post by AbraCadaver »

Well, they have to be related by something. I would say this as a simple example (assuming an image may have multiple caption files?):

Code: Select all

foreach(glob("images/*.*") as $image) {
	foreach (glob("captions/$image/*.*") as $caption) {
		echo file_get_contents($caption);
	}
	echo '<li><img src="'.$image.'" alt="random image"><div>something here'.'</div></li>'."&nbsp;&nbsp;";
} 
Now, in images/ you would have image1.jpg, testimage.png, etc.. and in captions/ you would have subdirs image1.jpg/, testimage.png/, etc.. that contain the caption files.

This is just a simple example that may not be scalable.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

I'm not sure I'm getting the captions/ file structure. Could you explain it differently?
kyleiwaniec
Forum Newbie
Posts: 14
Joined: Tue Jun 22, 2010 2:21 pm

Re: read multiple text files from directory

Post by kyleiwaniec »

Thank you for all your help. I got it working the way I want it by utilizing some css.
http://www.mercercountyrealtors.com/pre ... ential.php

Here is my final code:

Code: Select all

<html>
<head>
<title>Demo</title>
<style>
ul {
	list-style:none;


}

li {
	height:150px;
	width:150px;
	border:1px solid #06C;
}

#leftCol{
	float:left;	
	
}
#rightCol{
	float:left;	
	
}
.left{
	background-color:#360;
}
.right{
	background-color:#c50;
}

	</style>
</head>
<body>

<div id="leftCol">

<ul class="left">
  <?php 
  
  $files = glob("images/*.*"); 
 	 for ($i=0; $i<count($files); $i++) { 
 		$num = $files[$i]; 
  	
		
	//print $num."<br />";
	echo '<li><img src="'.$num.'" ></li>'; } 
	


		 	
	?>
</ul>
</div>
<div id="rightCol">
<ul class="right">
	<?php
	foreach (glob("captions/*.*") as $file) {
        echo '<li>';
		echo file_get_contents($file);
		echo '</li>';
}
	?>
</ul>
</div>

<br style="clear:both;">

</body>
</html>
Post Reply