pikachu Posted June 13, 2008 Share Posted June 13, 2008 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. <?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); } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 13, 2008 Share Posted June 13, 2008 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
pikachu Posted June 13, 2008 Author Share Posted June 13, 2008 Thank you very much for your help! Unfortunately using this code I recieve an error "Parse error: syntax error, unexpected T_FOREACH..." :'( Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
pikachu Posted June 13, 2008 Author Share Posted June 13, 2008 <?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. :'( Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 Any more errors? We need anything like that if we're going to help Quote Link to comment Share on other sites More sharing options...
pikachu Posted June 13, 2008 Author Share Posted June 13, 2008 no, no errors. just no content (blank page). ??? Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 You have changed the $dir variable to the path to your directory? Quote Link to comment Share on other sites More sharing options...
pikachu Posted June 13, 2008 Author Share Posted June 13, 2008 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) :'( :'( Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 Hmm, check in the source of the output in your browser, is there anything there? If so, post it. Quote Link to comment Share on other sites More sharing options...
pikachu Posted June 13, 2008 Author Share Posted June 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 If(something) { etc } Is the syntax, though you dont need to use curly brackets if there's only one line to the if... if if if if Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.