shelley3 Posted June 4, 2013 Share Posted June 4, 2013 Can't seem to find a solution to this: I want to use the following readdir code: ++++++++++++ <?php if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "sort.php" && $entry != "sort2.php" && $entry != "index.php") { echo "<a href='$entry'>$entry</a><br />"; } } closedir($handle); } ?> ++++++++++++ The above code works perfectly to display all the files in my directory. (All the files to be displayed on my webpage are .pdf files (the code above excludes the .php files in the directory).) These pdf files consist of multiple Catalog_date.pdf, Schedule_date.pdf and Newsletter_date.pdf files, each of which category I want to place in a separate column. To do this, I have three CSS column class <div> sections. I will put the above code in each of the three <div class> sections and exclude two of the three categories depending on which column it is. I was using another code to do this that works perfectly in all respects. It lists the separate categories in separate columns. (see image file below): ++++++++++++ <?php $string = 'Catalog'; echo ""; $PATH = $_ENV['DOCUMENT_WWW']; $d=$PATH.'.'; $files=array(); $dir = opendir($d) or die("couldn't open directory"); while ($f = readdir($dir)) { if (!eregi("\.jpg",$f) && $f!=='.' && $f!=='..' && eregi("$string",$f) && eregi("\.pdf",$f)){ #if does not match .jpg . and .. array_push($files,"$f"); echo "<a href='$f'>$f</a><br>"; } if( is_dir($_dir)) closedir( $d );} ?> ++++++++++++ But "eregi" has been deprecated and I wanted to update my php code to account for this. However, I don't know how to simulate the "f (!eregi("\.jpg",$f) && $f!=='.' && $f!=='..' && eregi("$string",$f) && eregi("\.pdf",$f))" part that allows me to fill the Catalog column with only the Catalog_date.pdf files, etc. (I have attached my currently working page that uses the older code as the example of what I want to do.) Can anyone help me with either including only those files that begin with the word Calendar_, Schedule_ or Newsletter_ for each script; or excluding two of the three files that begin with those words? I have looked at the info in the PHP Manual as well as other Googled places, but don't find enough information to make the code function. Hope this makes sense. Any assistance greatly appreciated. Shelley Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/ Share on other sites More sharing options...
requinix Posted June 4, 2013 Share Posted June 4, 2013 Do you want to show all the *.pdf files? There's a much simpler way to do this: <?php $files = glob($d . "/*.pdf"); foreach ($files as $f) { echo "<a href='{$f}'></a><br />"; } ?> glob For just one type of file use (eg) "/Catalog_*.pdf". Otherwise if it's literally those four types (and you want them all together) then same as above but using $files = glob($d . "/{Catalog,Calendar,Schedule,Newsletter}_*.pdf", GLOB_BRACE); Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434138 Share on other sites More sharing options...
shelley3 Posted June 5, 2013 Author Share Posted June 5, 2013 Wow, thanks so much Guru and requinix. I'll give this a try and mark it solved when I get it working. Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434322 Share on other sites More sharing options...
shelley3 Posted June 7, 2013 Author Share Posted June 7, 2013 Good Morning. The above code worked perfectly for me with one exception: When the code returns the files to the page, it includes a "/." before each title. How do I remove this prefix. Many thanks for help with this. Here is my code: <?php $d = "."; $handle = opendir($d); $files = glob($d .'/Catalog_*.pdf'); if ($handle) { foreach ($files as $f) { echo "<a href='{$f}'>$f</a><br />"; } closedir($handle); } ?> This returns: ./Catalog_* (whatever the title is) Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434692 Share on other sites More sharing options...
Christian F. Posted June 7, 2013 Share Posted June 7, 2013 str_replace will help you with that. Also, with glob you don't need opendir. I recommend following the links and studying the examples in the PHP manual. Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434695 Share on other sites More sharing options...
AbraCadaver Posted June 7, 2013 Share Posted June 7, 2013 $f = basename($f); Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434716 Share on other sites More sharing options...
shelley3 Posted June 8, 2013 Author Share Posted June 8, 2013 Many thanks to all of you (requinix, AbraCadaver and Christian F.) for your help with my questions. All is now working. Here is my final code: <?php $d = "."; $files = glob($d .'/Catalog_*.pdf'); krsort($files); if ($d) { foreach ($files as $f) { $f = basename($f); echo "<a href='{$f}'>$f</a><br />"; } closedir($d); } ?> Code is repeated in three CSS columns, each with a different Title_* so readers can choose the archives they need. So neat! Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434862 Share on other sites More sharing options...
requinix Posted June 8, 2013 Share Posted June 8, 2013 Get rid of the closedir() too. Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1434897 Share on other sites More sharing options...
shelley3 Posted June 10, 2013 Author Share Posted June 10, 2013 Done! Thanks. Obviously have to read up on glob(). So here is the final final: <?php $d = "."; $files = glob($d .'/Publication_*.pdf'); krsort($files); if ($d) { foreach ($files as $f) { $f = basename($f); echo "<a href='{$f}'>$f</a><br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1435157 Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 krsort() sorts based on the keys of the array. $files will just have useless numeric keys. You want rsort. Also your if ($d) won't help anything. All you're doing is checking whether $d has a value. It definitely does. If anything you'd check whether $files has anything, but you can let the foreach do that for you (since you don't do anything else). Unraveling a little bit more, if you're doing a basename() and providing a link to the file, the script will only work for $d=the current directory. If you provide anything else then you'll have to remove the basename for the link to work. Meanwhile glob() doesn't need to know about the ./ path since it'll default to that anyways. Removing that means you don't need basename() at all. And now you're left with <?php // $d = ""; if you really want something // if not the current directory then include the trailing slash, like // $d = "subdirectory/"; // so that the filename pattern you give to glob() remains only the filename $files = glob('Publication_*.pdf'); // $d . 'Publication_*.pdf' rsort($files); foreach ($files as $f) { echo "<a href='{$f}'>{$f}</a><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1435168 Share on other sites More sharing options...
shelley3 Posted June 12, 2013 Author Share Posted June 12, 2013 Perfect! Thank you SO much for taking the time to explain all this, requinix. Really clean, efficient coding! Link to comment https://forums.phpfreaks.com/topic/278780-readdir-question-how-to-exclude-files-whose-names-begin-with-a-certain-word/#findComment-1435559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.