John Beasley
John Beasley

Reputation: 3075

List files in same directory using PHP

I'm trying to list files in a folder. I have done this before, so I am not sure why I am having a problem now.

I have a PDF files I am trying to display to my web page. The directory structure looks like this:

folder1/folder2/displayFiles.php
folder1/folder2/files.pdf

displayFiles.php is the process file where I am using the code below.

I am trying to display the file called files.pdf onto the page, which is in the same directory as the process file.

Here is my code so far:

 <?php
   $dir = "folder1/folder2/"; 
   // $dir = "/";  <-- I also tried this
   $ffs = scandir($dir);

   foreach($ffs as $ff)
   {
     if($ff != '.' && $ff != '..')
     {
       $filesize = filesize($dir . '/' . $ff);  
       echo "<ul><li><a download href='$dir/$ff'>$ff</a></li></ul>";
     }
   }
 ?>

I know it's a simple fix. I just cannot find the code to fix it.

Upvotes: 0

Views: 187

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

Your $dir is pointing at a non-existent folder

Change the dir to point to the folder correctly $dir = ".";.

Upvotes: 1

user3119231
user3119231

Reputation:

Just use glob

http://php.net/manual/de/function.glob.php

$pdfs = glob("*.pdf"); // if needed loop through your directorys and glob files
print_r($pdfs);

Just an example. You should be able to use it with some edits.

Upvotes: 1

Related Questions