Ole Sørensen
Ole Sørensen

Reputation: 359

path works from root only

I read through all I could find on the topic, but nothing worked for me. I run PHP 5.3.29.

I have this PHP code, that works from root, but not from folders:

  // Fetch latest poster
  $base_url = 'posters';
  $newest_mtime = 0;
  if ($handle = opendir($base_url)) {
  while (false !== ($latestFile = readdir($handle))) {
      if (($latestFile != '.') && ($latestFile != '..')) {
         $mtime = filemtime("$base_url/$latestFile");
         if ($mtime > $newest_mtime) {
            $newest_mtime = $mtime;
            $show_file = "$base_url/$latestFile";
         }
      }
    }
  }                  
  // Show latest poster
  $show_name = basename(trim($show_file, ".jpg"));
  echo '
  <a href="/' . $show_file . '">
  <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '">
  </a>';

I'd love to just add a slash to get root-relative paths, but that doesn't work in PHP.

This is the site with this issue:

http://flamencopeko.net (here there is no issue)

http://flamencopeko.net/disco_pages/sundays.php (this one has the issue)

Upvotes: 1

Views: 205

Answers (4)

Ole S&#248;rensen
Ole S&#248;rensen

Reputation: 359

Big thanks again.

From root that resulted in:

Warning: opendir(/home/flamenco/public_htmlposters) [function.opendir]: failed to open dir: No such file or directory in /home/flamenco/public_html/left_test.php on line 40

so I changed

base_url = getcwd() . 'posters';

to

base_url = getcwd() . '/posters';

This shows the poster, but it links to:

http://flamencopeko.net/http://flamencopeko.net/posters/08-05-2017_01.jpg

From sub-folder it results in:

Warning: opendir(/home/flamenco/public_html/disco_pages/posters) [function.opendir]: failed to open dir: No such file or directory in /home/flamenco/public_html/disco_pages/left_test2.php on line 12

I was able to write a messy work-around, that works, just now:

$path = getcwd();
if ($path == '/home/flamenco/public_html') {
  $base_url = 'posters';
  $newest_mtime = 0;
  if ($handle = opendir($base_url)) {
  while (false !== ($latestFile = readdir($handle))) {
      if (($latestFile != '.') && ($latestFile != '..')) {
         $mtime = filemtime("$base_url/$latestFile");
         if ($mtime > $newest_mtime) {
            $newest_mtime = $mtime;
            $show_file = "$base_url/$latestFile";
         }
      }
    }
  }
  // Show latest poster
  $show_name = basename(trim($show_file, ".jpg"));
  echo '
  <a href="/' . $show_file . '">
  <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '">
  </a>';
  }
  else
  {
  $base_url = '../posters';
  $newest_mtime = 0;
  if ($handle = opendir($base_url)) {
  while (false !== ($latestFile = readdir($handle))) {
      if (($latestFile != '.') && ($latestFile != '..')) {
         $mtime = filemtime("$base_url/$latestFile");
         if ($mtime > $newest_mtime) {
            $newest_mtime = $mtime;
            $show_file = "$base_url/$latestFile";
         }
      }
    }
  }
  // Show latest poster
  $show_name = basename(trim($show_file, ".jpg"));
  echo '
  <a href="/' . $show_file . '">
  <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '">
  </a>';
  }

Upvotes: 0

Darren
Darren

Reputation: 13128

The reason you can only display the images on the root dir has nothing to do with your script. It has to do with the url's your placing in your <img.. tag, like below;

Root

<img src='/posters/NAME_OF_IMG.jpg' ... />

Which would resolve to something like: http://www.yourwebsite.com/posters/NAME_OF_IMG.jpg

Sub Dir Which would resolve to something like: http://www.yourwebsite.com/disco_pages/posters/NAME_OF_IMG.jpg - WHICH DOESN't EXIST.

What you need to do is modify your code to generate the $show_file url like this:

$base_url = getcwd() . 'posters';
$newest_mtime = 0;
if ($handle = opendir($base_url)) {
while (false !== ($latestFile = readdir($handle))) {
    if (($latestFile != '.') && ($latestFile != '..')) {
       $mtime = filemtime("$base_url/$latestFile");
       if ($mtime > $newest_mtime) {
            $newest_mtime = $mtime;
            $show_file = "http://www.yourdomain.com/posters/" . $latestFile;
       }
    }
  }
}

Alternatively, you could use $_SERVER['DOCUMENT_ROOT'] instead of getcwd() if required.

This should work for you from every directory you attempt. You have to understand what your code is doing to understand why it isn't working as you want.

  1. You're traversing through your directories at file level to find the newest file, correct? Hence you need the absolute path of the file to check these aspects.
  2. You're providing that absolute path to the <img ... tag which won't work. Your website is expecting the relative path (http://www.yourwebsite.com/posters/THE_IMAGE_NAME.jpg).

Upvotes: 1

Arun
Arun

Reputation: 1719

Change your $base_url to

$base_url = $_SERVER['DOCUMENT_ROOT'].'/posters';  

To get root directory path of a PHP project:

For PHP >= 5.3.0

use: DIR

Note: The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory.

For PHP < 5.3.0

use: dirname(FILE) or realpath(dirname(FILE))

Or in most common for getting server document root directory where projects resides :

$_SERVER['DOCUMENT_ROOT'] or filter_input(INPUT_SERVER, 'DOCUMENT_ROOT')

Upvotes: 2

Difster
Difster

Reputation: 3270

I think where you want to start is $_SERVER['DOCUMENT_ROOT']

Upvotes: 1

Related Questions