Home > Net >  How to display latest 4 images from mounted network folder in php?
How to display latest 4 images from mounted network folder in php?

Time:12-31

I have used symlinks to fetch mounted network folder in the specified path "/var/www/Access/images".As the beginner tried certain piece of code but it is displaying broken image.I think there is issue in path format,but unable to figure out.Please any one suggest.

`<?php
  // Open the directory
  $dir = opendir('/var/www/Access/images');
  // Initialize an array to store the file names and last modified times
  $files = array();

  // Read the contents of the directory
  while (($file = readdir($dir)) !== false) {
    // Exclude the current and parent directories
    if ($file == '.' || $file == '..') continue;

    // Check if the file is a directory
    if (is_dir("/var/www/Access/images/'$file")) continue;

    // Get the last modified time for the file
    $lastModified = filemtime("/var/www/html/Access/images/'$file");

    // Add the file name and last modified time to the array
    $files[$file] = $lastModified;
  }

  // Close the directory
  closedir($dir);

  // Sort the array by last modified time
  asort($files);
  $files=array_reverse($files);
  // Initialize a counter
  $count = 0;

  // Iterate through the sorted array
  foreach ($files as $file => $lastModified) {
    // Display the image
    echo "<img src='/var/www/Access/images/'$file'>";

    // Increment the counter
    $count  ;

    // Stop displaying images after the fourth one
    if ($count == 2) break;
  }
?>`
``

CodePudding user response:

browser can not recognize path except the root of web server.

for example if /var/www/project/ is your source containg index.php that web server load it , browser can read data only in this directory and subdirectory such /var/www/project/access/images/img.png

to set src attribute of <img> you can accomplish this using:

  • absoulte path : https://example.com/access/images/img.png
  • relative path : access/images/img.png

I hope this works

  •  Tags:  
  • php
  • Related