How to Display Images from a Folder Using PHP - PHP

Pull all images from a specified directory and then display them

You can also use glob for this:

$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");

foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

How to display images from a folder using php - PHP

You had a mistake on the statement below. Use . not ,

echo '<img src="', $dir, '/', $file, '" alt="', $file, $

to

echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. $

and

echo 'Directory \'', $dir, '\' not found!';

to

echo 'Directory \''. $dir. '\' not found!';

how to display an image from a directory in PHP?

<?php
$dir="./img";
if(is_dir($dir)){
$files=scandir($dir);
unset($files[array_search('.',$files)]);
unset($files[array_search('..',$files)]);
$sn=0;
foreach($files as $key=>$val){
echo "SNo: ".++$sn."<br/>\n";
echo "Filename: ".$val."<br/>\n";
echo "Date-Time Modified: ".date('Y/m/d h:i',filemtime(rtrim($dir,'\/')."/".$val))."<br/>\n";
echo "Filesize: ".filesize(rtrim($dir,'\/')."/".$val)."bytes<br/>\n";
echo "<img src=\"".rtrim($dir,'\/')."/".$val."\" /><br/><br/>\n\n";
}
}else{
echo "(".$dir.") does not exist or is not a valid directory";
}
?>

How to display an image from a folder using PHP variable in a HTML tag?

I think this one will help you

<img src="uploads/<?php echo $file.png;?>" />

Display images from folder in table using php,html?

If you want it in table the simply add table tag with and to it

<table>
<tr>
<th>Image Name</th>
<th>Image</th>
</tr>
<?php
$files = glob("images/*.*");
for ($i = 0; $i < count($files); $i++) {
$image = $files[$i];
$supported_file = array(
'gif',
'jpg',
'jpeg',
'png'
);

$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo "<tr><td>";
echo basename($image);
echo "</td><td>";
echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';
echo "</td></tr>";
} else {
continue;
}
}
?>
</table>

Displaying the images in a folder using php

I am assuming your csv file data are a array of file names separated by new line like the following:

some-img-01.jpg
img-02.jpg
img-05.jpg
....

Please check with the following codes.
I have restructured the code to pass the state(if exists from previous post) between each page loads. Here I have designed it to cycle - from first if reaches the end and next is clicked or jump to last if previous is clicked from first position. You can disable this features by commenting out the appropriate lines(check the comments).

<?php
$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;

$fp = fopen('imgname.csv', 'r');
$line = fread($fp, filesize('imgname.csv'));
fclose($fp);
$item = explode("\n", $line);
$sizecsv = sizeof($item);

$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;

if ($current_index >= $sizecsv) $current_index = ($sizecsv - 1);
if ($current_index < 0) $current_index = 0;

if (isset($_POST['next'])) {
$current_index++;
// If reached end of the list, then clicked next, the index starts from firt again.
// If you dont need this feature, then can comment the following line.
if ($current_index >= $sizecsv) $current_index = 0;
}
if (isset($_POST['previous'])) {
$current_index--;
// If the indexin first image, but clicked the Previous button, then the index goes to last element.
// If you dont need this feature, then can comment the following line.
if ($current_index < 0) $current_index = ($sizecsv - 1);
}
if (isset($_POST['first'])) $current_index = 0;
if (isset($_POST['last'])) $current_index = ($sizecsv - 1);
?><!DOCTYPE html>
<html>
<head>
<title>Your Album</title>
</head>
<body>
<form method="post">
<input type="text" name="current_index" value="<?php echo $current_index;?>"/>
<input type="submit" name="first" value="FIRST"/>
<input type="submit" name="previous" value="PREVIOUS"/>
<input type="submit" name="next" value="NEXT"/>
<input type="submit" name="last" value="LAST"/>
<input type="submit" name="dele" value="DELETE"/>
</form>
<br>

<img src="images/<?php echo $item[$current_index];?>" width="250px"/>
<br>, Image No: <?php echo ($current_index + 1);?>
</body>
</html>

how to display an image from folder with php?

Seems path issue.

I can see that you are inserting image path in table something like 'assets/news_image/$fileName' but you are uploading image '../assets/news_image/'.

Update file path to :-

For absolute path (recommended) :

echo "<img src="."/". $row['image'] . " />";

or

For relative path:

echo "<img src="."../". $row['image'] . " />";

Hope this will help you (y).

how to display all images in a folder

$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");

foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}


Related Topics



Leave a reply



Submit