1

I am trying to return a user's AD photo along with their name and job title. I can pull any of the text fields ok but it errors when i try to pull the "thumbnailPhoto" attribute. Admittedly I am using a template that I found however i'm confused how most attributes seem to work apart from the photo.

This is the error that I currently get when trying to run the below code

Notice: Undefined index: thumbnailphoto in C:\xampp\htdocs\visitor\ldap-test.php on line 23

Any help would be appreciated.

    <?php
// -------------- CHANGE VARIABLES TO SUIT YOUR ENVIRONMENT  --------------
//LDAP server address
$server = "ldap://192.168.1.55";
//domain user to connect to LDAP
$user = "user.name@mydomain.local";
//user password
$psw = "password";
//FQDN path where search will be performed. OU - organizational unit / DC - domain component
$dn = "OU=Accounts,OU=My Company,DC=mydomain,DC=com";
//Search query. CN - common name (CN=* will return all objects)
$search = "CN=*";                    
// ------------------------------------------------------------------------
echo "<h2>php LDAP query test</h2>";
// connecting to LDAP server
$ds=ldap_connect($server);
$r=ldap_bind($ds, $user , $psw); 
// performing search
$sr=ldap_search($ds, $dn, $search);
$data = ldap_get_entries($ds, $sr);    
echo "Found " . $data["count"] . " entries";
for ($i=0; $i<$data["count"]; $i++) {
 echo "<h4><strong>Common Name: </strong>" . $data[$i]["cn"][0] . "</h4><br />";
 echo "<strong>Distinguished Name: </strong>" . $data[$i]["dn"] . "<br />";
 //checking if discription exists 
 if (isset($data[$i]["description"][0])) 
 echo "<strong>Desription: </strong>" . $data[$i]["description"][0] . "<br />";
 else 
 echo "<strong>Description not set</strong><br />";
 //checking if email exists
 if (isset($data[$i]["mail"][0]))
 echo "<strong>Email: </strong>" . $data[$i]["mail"][0] . "<br /><hr />";
 else 
 echo "<strong>Email not set</strong><br /><hr />";
 }
 // close connection
 ldap_close($ds);
?>
  • `...it errors when i try to...` What's the error? – Jonathan Jun 15 '18 at 18:29
  • "Notice: Undefined index: thumbnailPhoto in C:\xampp\htdocs\visitor\ldap-test.php on line 23" I am replacing the 'cn' part with thumbnailPhoto. I have done some research and I will need to put it in an image tag etc but I am expecting to at least see the hex values at this stage. – Rhys Williams Jun 15 '18 at 18:35

1 Answers1

0

Try to run this code example:

<?php

// -------------- CHANGE VARIABLES TO SUIT YOUR ENVIRONMENT  --------------
//LDAP server address
$server = "ldap://192.168.1.55";
//domain user to connect to LDAP
$user = "user.name@mydomain.local";
//user password
$psw = "password";
//FQDN path where search will be performed. OU - organizational unit / DC - domain component
$dn = "OU=Accounts,OU=My Company,DC=mydomain,DC=com";
//Search query. CN - common name (CN=* will return all objects)
$search = "CN=*";
// ------------------------------------------------------------------------

echo "<h2>php LDAP query test</h2>";
// connecting to LDAP server
$ds = ldap_connect($server);
$r = ldap_bind($ds, $user , $psw);
// performing search
$sr = ldap_search($ds, $dn, $search);
$data = ldap_get_entries($ds, $sr);

echo "Found " . $data["count"] . " entries";

for ($i = 0; $i < $data["count"]; $i++) {
    echo "<h4><strong>Common Name: </strong>" . $data[$i]["cn"][0] . "</h4><br />";
    echo "<strong>Distinguished Name: </strong>" . $data[$i]["dn"] . "<br />";

    // Check if user photo exists
    if (isset($data[$i]["thumbnailphoto"]) && isset($data[$i]["thumbnailphoto"][0])) {
        echo "<strong>Photo in Base64: </strong>" . base64_encode($data[$i]["thumbnailphoto"][0]) . "<br />";
    }
    else {
        echo "<strong>Photo not set</strong><br />";
    }

    // Checking if discription exists 
    if (isset($data[$i]["description"][0])) {
        echo "<strong>Desription: </strong>" . $data[$i]["description"][0] . "<br />";
    }
    else {
        echo "<strong>Description not set</strong><br />";
    }

    // Checking if email exists
    if (isset($data[$i]["mail"][0])){
        echo "<strong>Email: </strong>" . $data[$i]["mail"][0] . "<br /><hr />";
    }
    else {
        echo "<strong>Email not set</strong><br /><hr />";
    }
}

// close connection
ldap_close($ds);
?>

The problem should be that you check if $data[$i]["thumbnailphoto"][0] is set while you should check the $data[$i]["thumbnailphoto"] first.

Then you can read this question Display thumbnailPhoto from Active Directory in PHP and continue coding to display the image.

GramThanos
  • 3,572
  • 1
  • 22
  • 34