note 76528 deleted from function.file-put-contents by nlopess

From: Date: Sat, 01 Sep 2007 19:02:34 +0000
Subject: note 76528 deleted from function.file-put-contents by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130071@lists.php.net to get a copy of this message
Note Submitter: jcwebb at dicoe dot com ---- i needed to scan a folder for any pdf type documents, and make a summary file. Later i needed to display the list as links. os= win & *nix. php=v5. <?php // definitions ---------------------------------- $pdfslocation=$_SERVER['DOCUMENT_ROOT']."/pdfs/"; $pdfslist=$pdfslocation.'alist.txt'; $readlist=''; $writelist=''; $str=''; $file=''; $filenames=array(); // Rebuild List --------------------------------- if (isset($_POST['Brefresh'])): if (file_exists($pdfslist)): $do=unlink($pdfslist); endif; $readlist=opendir($pdfslocation); while (false!==($file=readdir($readlist))): //manual says include "false!==" to avoid errors which stop loop if (substr($file,-3)=='pdf'): $str.=$file."\r\n"; endif; // previous line *MUST* be double quotes (") not single quotes (') - this really caused me headaches on windows! endwhile; closedir($readlist); $result=file_put_contents($pdfslist,$str); if ($result<1 or result==false): echo 'error writing: '.$result; endif; endif; ?> <?php // Make array of file names ----------------- $filenames=array_map('rtrim',file($pdfslist)); #rtrim fixes \r\n on windows. & still works normally on *nix. (we dont want eol in array) // create list of links ------------------------ foreach ($filenames as $filename): echo '<a href="'.$filename.'" target="_blank">'.$filename.'</a><br />'; endforeach; ?> i hope this helps others, Jon And thanks to everyone for your examples/comments.

« previous php.notes (#130071) next »