Jump to content

listing files in subdirectories.


pikachu

Recommended Posts

I'm fairly new to php and not familiar enough with it to solve this problem on my own.  I'm hoping someone here can help!

 

I have a folder filled with subfolders that contain various pdf files.  It is constantly being updated changed.  I would like my php code to scan the directory and do the following: 1. folder names will become Categories, but are not clickable.  2. pdf files that are contained in each folder are listed along with a link. (underneath the appropriate category)

 

Using google, I found a script that ALMOST does what I want, but it is not exact enough. The folder I would like to scan is a subfolder but the script I found scans the root folder.  How do I tell it to scan ONLY a specific folder and its subfolders?

 

Also, I would not like any php files displayed in the content that is output. I only would like pdf files to be shown.

 

Thanks in advance to anyone who takes the time to read my nonsense.  :-*;D

 

<?php 
  // grab a full file listing from the current and sub directories and show them as anchored 
//(linked) files. 

  // pull a full file listing - requires the Unix 'find' and 'sort commands.  'find' will retrieve a 
//list of all files from the current directory, 'sort' will sort the listing, and 'explode' will split 
//all files into an array passed into $filelist. 

  $filelist = explode("\n",`find .|sort`); 

  // for each item (file) in the array... 

  for ($count=0;$count<count($filelist);$count++) { 

  // get the filename (including preceding directory, ie: ./software/gth1.0.9.tar.gz) 

  $filename=$filelist[$count]; 

   // if it's not a directory, display linked 

    if (!is_dir($filename)) 
      printf("<a href=\"%s\">%s</a><br>\n",$filename,$filename); 

   // otherwise tell the user it's a "category" 

    else printf("<p>Category: %s<p>\n",$filename); 
  } 

Link to comment
Share on other sites

how about something like this (not tested):

 

<?php
  $dir = 'path/to/folder/';
  if(!is_dir($dir)) die("Invalid directory");
  $dir = realpath($dir)
  foreach(glob('*',GLOB_ONLYDIR) as $cat){
    $subdir = $dir.'/'.$cat;
    if(!is_dir($subdir)) continue;
    echo "<h2>{$cat}</h2><ul>";
    foreach(glob($subdir.'/*.pdf') as $file)
      echo '<li><a href="'.$subdir.'/'.$file.'">'.$file.'</a></li>';
    echo '</ul>';
  }
?>

Link to comment
Share on other sites

<?php
  $dir = 'path/to/folder/';
  if(!is_dir($dir)) die("Invalid directory");
  $dir = realpath($dir); //missed a semi-colon
  foreach(glob('*',GLOB_ONLYDIR) as $cat){
    $subdir = $dir.'/'.$cat;
    if(!is_dir($subdir)) continue;
    echo "<h2>{$cat}</h2><ul>";
    foreach(glob($subdir.'/*.pdf') as $file)
      echo '<li><a href="'.$subdir.'/'.$file.'">'.$file.'</a></li>';
    echo '</ul>';
  }
?>

That should work.

Link to comment
Share on other sites

<?php
  $dir = 'path/to/folder/';
  if(!is_dir($dir)) die("Invalid directory");
  $dir = realpath($dir); //missed a semi-colon
  foreach(glob('*',GLOB_ONLYDIR) as $cat){
    $subdir = $dir.'/'.$cat;
    if(!is_dir($subdir)) continue;
    echo "<h2>{$cat}</h2><ul>";
    foreach(glob($subdir.'/*.pdf') as $file)
      echo '<li><a href="'.$subdir.'/'.$file.'">'.$file.'</a></li>';
    echo '</ul>';
  }
?>

That should work.

it would be totally awesome if it did.  i wish i was smart enough to figure out why it doesnt.  :'(
Link to comment
Share on other sites

yes, i changed it to the correct path.  I'm not getting any errors at all, in fact i think its even doing something since the page takes a split second to load.  but then... white space.  my heart sinks in despair (melodrama)  :'( :'(

Link to comment
Share on other sites

Theres nothing in the output.  I put the php code in a div tag (to control overflow) and the div is completely empty/blank.

 

I was thinking that maybe the code is stuck looping or something and never finishes...  I was always used to the syntax if(something) then (somethingelse).  or maybe theres some brackets missing?  I feel like beating my face into a wall until it's bloody... Ive been trying to do this (seemingly) simple thing for a few days now.  Maybe it's time to go outside.  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.