fread gets one too many (makes a null) in array<

liunx

Guest
Hi there,

I have a simple online photo album that works by reading a text file with all the image names (seperated by ';') and then using fopen and fread to put all these images names into an array. The images themselves are in the same directory as the corresponding txt file.

The album works, except that it always adds one extra member of the array, which of course does not have a corresponding image to put online.

Here is what the text file (group.txt) looks like:

DSCN0373.jpg;
DSCN0374.jpg;
DSCN0375.jpg;
DSCN0376.jpg;
DSCN0377.jpg;
DSCN0378.jpg;
DSCN0379.jpg;
DSCN0380.jpg;
DSCN0381.jpg;
DSCN0382.jpg;

Here is what the image directory looks like:

DSCN0372.jpg
DSCN0373.jpg
DSCN0374.jpg
DSCN0375.jpg
DSCN0376.jpg
DSCN0377.jpg
DSCN0378.jpg
DSCN0379.jpg
DSCN0380.jpg
DSCN0381.jpg
DSCN0382.jpg
group.txt


Here is the PHP code that pulls the imagenames:


// Insert the photos here
// First open up the correct file and read the contents into an array
$file = $section . "/" . $section . ".txt";
$route = $path.$file;
$handle = fopen( $route, "r" );
$images = fread( $handle, filesize( $route ) );
$imagenames = explode( ";",$images );
print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\">\n";
foreach ($imagenames as $value) {
print "<tr><td><img src=http://www.htmlforums.com/archive/index.php/\"/images/meeting/photos/$section/$value\" border=\"1\" />\n<br/>$value<br/></td></tr>\n";
}
print "</table>\n";


Thanks in advance for the help.

- Tatlarseems like there is a extra line at the bottom of that txt file. how are you saving that file? doesn't matter how it is read, it is putting an extra space in the file I bet.


if the space is always the last one then you could use array_pop() to take it off before you use the loop on it.im guessing your problem is having the ; at the end of the last entry in the file. if you take that out, it will work. if it has to be there, do what scoutt said and use array_popn8thegreat,

thanks very much - that solved the problem.why even have that at the ends of that? the way you insert them into the file can be questioned too. but if it works for you then that is fine.yeah, if i were you i would just sepereate them by a newline and then use file() to automatically read it and put it into an array. its the best way for stuff like that
 
Back
Top