The error you are getting is sayings the path is not found, it has to exists first. The second problem is that you are using the code the wrong way even when the path is fixed.
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$display_models .= '<li>';
$display_models .= '<a href="profilepage/'.$row->id.'">';
$display_models .= '<img src="'. $dir.'/'. $file.'">';
$display_models .= '</a>';
$display_models .= '</li>';
}
closedir($dh);
}
}
You will want something similar to the above, that is how you use that function.
Remember that the path needs to be fixed first otherwise this will not loop through anything because the folder was not found and it will not find any files to loop through.