0

I have a directory inside my server full of PDF files and I would like to open the file and be able to click on the name of the PDF and watch its content. Now I only get the names of the files in a list but thats all. I would like to press in the name and open the PDF.

<?php 
$sub = ($_GET['dir']); 
$path = 'pedidos/'; 
$path = $path . "$sub"; 
$dh = opendir($path); 
$i=1; 
while (($file = readdir($dh)) !==   false) {
  if($file != "." && $file != "..") {
      if (substr($file, -4, -3) =="."){
          echo "$i. $file <br />";
      }else{                  
          echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
      }
      $i++;
   } } 
closedir($dh); ?>
Eyal Alsheich
  • 452
  • 4
  • 12
user1757383
  • 91
  • 1
  • 10

2 Answers2

1

I modified your code and works for me:

<?php
$sub = ($_GET['dir']);
$path = 'machotes/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !==   false) {
    if($file != "." && $file != "..") {
        if (substr($file, -4, -3) =="."){
            echo "$i. <a href='$path/$file'>$file</a><br />";
        }
        $i++;
    }
}
closedir($dh);
?>
totymedli
  • 29,531
  • 22
  • 131
  • 165
Lalo Oceja
  • 47
  • 3
  • 12
0

your URL in the a href seems to be wrong try:

echo "$i. <a href='$path/$file'>$file</a><br />";

just make sure that $path is the correct relative path

this will link to the actual pdf file allowing the users pdf reader to get to it it will NOT parse and display the pdf using php

Eyal Alsheich
  • 452
  • 4
  • 12