Alchemist
Alchemist

Reputation: 73

how to display an image from a directory in PHP?

i'm trying to show a group of images from a local directory and also the size and date of creation i tried this but seem to not work

    <?php
$dir = "./images";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false)
        {
        echo "<img src='/images/$file'>";
        echo date (Y/m/d H: i, filemtime($file));
        echo filesize($file);

        }
        closedir($dh);
    }
}

?>

Upvotes: 1

Views: 1790

Answers (2)

Alchemist
Alchemist

Reputation: 73

there was some errors on filetime and file size

      <?php
  $dir = "./images";
  if (is_dir($dir)) {
      if ($dh = opendir($dir)) {
          while (($file = readdir($dh)) !== false)
          {
        echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"  />';
        echo date ('Y/m/d H:i',filemtime($dir. '/'. $file));
        echo filesize($dir. '/'. $file)." bytes";
        }
        closedir($dh);
    }
}
?>

Upvotes: 0

Nelson Rakson
Nelson Rakson

Reputation: 566

<?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";
}
?>

Upvotes: 2

Related Questions