note 76528 added to function.file-put-contents
| From: | jcwebb at dicoe dot com | Date: | Thu, 19 Jul 2007 10:37:42 +0000 |
| Subject: | note 76528 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128722@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 80.64.47.13
Probable Submitter: 88.19.24.42
----
Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php
Edit -- https://master.php.net/note/edit/76528
Del: integrated -- https://master.php.net/note/delete/76528/integrated
Del: useless -- https://master.php.net/note/delete/76528/useless
Del: bad code -- https://master.php.net/note/delete/76528/bad+code
Del: spam -- https://master.php.net/note/delete/76528/spam
Del: non-english -- https://master.php.net/note/delete/76528/non-english
Del: in docs -- https://master.php.net/note/delete/76528/in+docs
Del: other reasons-- https://master.php.net/note/delete/76528
Reject -- https://master.php.net/note/reject/76528
Search -- https://master.php.net/manage/user-notes.php