I´m trying to create an phonebook with LDAP and PHP and to also show the pictures (jpegphoto) in a table.
With the code below I get almost everyting to work, the results are displayed, the tempfiles for the jpegs are written to the harddrive. But it´s only the first jpeg that are shown and all the other pictures and tempfiles are 0 bytes!
I guest it´s someting about the ldap_next_entry but I can´t get it to work.
(I´m quite new with PHP and LDAP.)
Any ideas?
<?php
$ip = "1.2.3.4";
$dn="CN=test,dc=net";
if (!($ldap = ldap_connect($ip))) {
die ("Could not connect to LDAP server");
}
print "connected to <b>$ip</b><br/>";
if (!($bind_id = @ldap_bind($ldap, $dn))) {
die ("Could not bind to $dn");
}
print "user <b>$dn</b> authenticated.<br/>";
// start searching
$params = array("cn", "jpegphoto", "homephone" );
$result = ldap_search($ldap,"dc=net", "(cn=*)",$params) or die ("Error in search query");
ldap_sort( $ldap, $result, "cn" );
// get entry data as array
$info = ldap_get_entries($ldap, $result);
$entry = ldap_first_entry($ldap,$result);
echo "<font size=4><b>Addressbook</b></font>";
echo "<table border=1 cellspacing=0 cellpadding=4 ><tr>";
echo "<td align=left><b> </b></td>";
echo "<td align=left><b>Name</b></td>";
echo "<td align=left><b>Phone</b></td>";
for ($i=0; $i<$info["count"]; $i++) {
// photo
$values = ldap_get_values_len($ldap, $entry, "jpegphoto");
$jpeg_filename = $jpeg_temp_dir . basename( tempnam ('.', 'djp') );
$outjpeg = fopen($jpeg_filename, "wb");
if( ! $outjpeg )
echo "error <br>";
fwrite($outjpeg, $values[$i]);
fclose ($outjpeg);
$entry = ldap_next_entry( $ldap, $result);
echo "<tr><td valign=top><img src=\"view_jpeg_photo.php?file=" . basename($jpeg_filename) . "\"></td>";
// name
echo "<td valign=top>". $info[$i]["cn"][0] ."<br></td>";
// phone
echo "<td valign=top>". $info[$i]["homephone"][0] ."</td>";
// end
}
echo "</tr></table>";
// print number of entries found
echo "<br>Posts: " . ldap_count_entries($ldap, $result) ."<br>";
// clean up
ldap_close($ldap);
?>
And the code for view_jpeg_photo.php:
<?php
$file = $_GET['file'];
$file = $jpeg_temp_dir . '/' . $file;
$file = basename( $file );
$file = addcslashes( $file, '/\' );
$f = fopen( "$file", 'r' );
$jpeg = fread( $f, filesize( "$file" ) );
fclose( $f );
Header( "Content-type: image/jpeg" );
Header( "Content-disposition: inline; filename=jpeg_photo.jpg" );
echo $jpeg;
?>