user1757383
user1757383

Reputation: 91

Open directory in server side and open files inside it

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); ?>

Upvotes: 0

Views: 4240

Answers (2)

Lalo Oceja
Lalo Oceja

Reputation: 57

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);
?>

Upvotes: 1

Eyal Alsheich
Eyal Alsheich

Reputation: 477

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

Upvotes: 0

Related Questions