0

I am using the below code to read files I have saved on my server in folders.

<?php
    if ($handle1 = opendir('../files/policies/fire')) {
        while (false !== ($file1 = readdir($handle1))) {
            if ($file1 != "." && $file1 != "..") {
                $thelist1 .= '<li><a href="'.$file1.'" target="_blank">'.$file1.'</a></li>';
            }
        }
        closedir($handle1);
    }
?>
<h3>Fire Fighting Files:</h3>
<ul><?php echo $thelist1; ?></ul></h6>

The code reads the files in the directory as it should. The issue I am having is once I click the link it searches or attempts to open the file in the root directory

It does not use the sub-dir to open the file but the root dir. The structure is as follow

/File
  /policies
    /FA

When I click the link it tries finding the file as below

/files/OHS_EHSR-020%20Employee%20Health%20and%20Safety%20Rules.pdf

and the file is saved as

/files/policies/FA/OHS_EHSR-020%20Employee%20Health%20and%20Safety%20Rules.pdf

Any suggestions would help. Please note my code I am using I took from this site as well

ADDING Full Code of two blocks this is why I cannot fix the directories to static in my code:

<div class="col-xl-5 col-md-5 col-12">
            <div class="box box-body">
              <h6 class="text-uppercase">
                  <?php
                  if ($handle = opendir('../files/policies/FA')) {
                      while (false !== ($file = readdir($handle))) {
                          if ($file != "." && $file != "..") {
                              $thelist .= '<li><a href="'.$file.'" target="_blank">'.$file.'</a></li>';
                          }
                      }
                      closedir($handle);
                  }
                  ?>
                  <h3>First Aid Files:</h3>
                  <ul><?php echo $thelist; ?></ul></h6>
             </div>
        </div>
        <!-- /.col -->
          <div class="col-xl-5 col-md-5 col-12">
              <div class="box box-body">
                  <h6 class="text-uppercase">
                      <?php
                      if ($handle1 = opendir('../files/policies/fire')) {
                          while (false !== ($file1 = readdir($handle1))) {
                              if ($file1 != "." && $file1 != "..") {
                                  $thelist1 .= '<li><a href="'.$file1.'" target="_blank">'.$file1.'</a></li>';
                              }
                          }
                          closedir($handle1);
                      }
                      ?>
                      <h3>Fire Fighting Files:</h3>
                      <ul><?php echo $thelist1; ?></ul></h6>
              </div>
          </div>
3
  • Does this work => '<li><a href="/files/policies/FA/'.$file1.'" target="_blank">'.$file1.'</a></li>'; ? Also, are you sure you would want to show actual file path of the server? Commented Oct 15, 2019 at 6:59
  • I do not want to show the path and I cannot fix the path as the files are in different directories Commented Oct 15, 2019 at 7:50
  • What happens if a user clicks the link? They download the file? Commented Oct 15, 2019 at 7:54

1 Answer 1

1

readdir returns relative path not absolute, so you need to prepend /files/policies to the $file variable and use it in your link

update:

$dir = '/files/policies/fire';

if ($handle1 = opendir('..' . $dir)) {
  while (FALSE !== ($file1 = readdir($handle1))) {
    if ($file1 != "." && $file1 != "..") {
      $filepath = $dir . DIRECTORY_SEPARATOR . $file1;
      $thelist1 .= '<li><a href="' . $filepath . '" target="_blank">' . $file1 . '</a></li>';
    }
  }
  closedir($handle1);
}

this should work for you

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.