display image in from folder in php code example
Example 1: display image in php from folder
<html>
<head>
<title>display image</title>
</head>
<body>
<p>Here in your form and text</p>
<?php
echo "<img src='image-name.png' >";
?>
</body>
</html>
Example 2: php list output images in a directory
<?php
echo scanDirectoryImages("uploads");
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
{
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
$html = '';
if (
is_readable($directory)
&& (file_exists($directory) || is_dir($directory))
) {
$directoryList = opendir($directory);
while($file = readdir($directoryList)) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_readable($path)) {
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
if (
is_file($path)
&& in_array(end(explode('.', end(explode('/', $path)))), $exts)
) {
$html .= '<a href="' . $path . '"><img src="' . $path
. '" style="max-height:100px;max-width:100px" /> </a>';
}
}
}
}
closedir($directoryList);
}
return $html;
}