X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 28628D1B549 for ; Sun, 26 Oct 2003 15:44:28 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 53798-02 for ; Sun, 26 Oct 2003 11:44:00 -0400 (AST) Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [192.108.102.143]) by svr1.postgresql.org (Postfix) with ESMTP id EDD51D1B564 for ; Sun, 26 Oct 2003 11:43:55 -0400 (AST) Received: from myrealbox.com grzm@smtp-send.myrealbox.com [218.218.223.125] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 3.43 $ on Novell NetWare; Sun, 26 Oct 2003 08:43:53 -0700 Date: Mon, 27 Oct 2003 00:43:49 +0900 Mime-Version: 1.0 (Apple Message framework v552) Content-Type: text/plain; charset=WINDOWS-1252; format=flowed Subject: Saving result to file for download From: Michael Glaesemann To: pgsql-php@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <315833F6-07CB-11D8-B456-0005029FC1A7@myrealbox.com> X-Mailer: Apple Mail (2.552) X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/25 X-Sequence-Number: 2386 Hi all! I made a PHP script that formats and saves the output of a query to a=20 file and provides a link to the file for download. I've never done this=20 before and would appreciate any feedback. Here are things I'd like to=20 improve or am concerned about. I've looked through Sklar & Trachtenberg's 'PHP Cookbook', googled=20 ('PHP postgresql saving result file download' gives you a wide=20 assortment of links!), and attempted to search the archives of=20 pgsql-php, but I keep timing out after 60 seconds. Has anyone else been=20 experiencing problems searching the archives? 1. Right now the file is permanently saved in a directory (used only=20 for saving these results files, unimaginatively named /temp) in the web=20 root of the server (in my case /Library/Webserver/Documents on Mac OS X=20 10.2). I'd rather it be a temporary file so I wouldn't have to worry=20 about clearing out the files if a lot of people generate results files.=20 I'm not concerned that people won't be able to come back to the results=20 file at a later date=97they can just generate a new one. Perhaps I should= =20 make a cron job to clear out the folder every once in a while? 2. Security. I've changed the owner on /temp to www (the webserver) so=20 that PHP can write to the directory. Here are the permissions. drwxr-xr-x 23 www admin 782 Oct 27 00:05 temp I'm guessing I should change the permissions to drwxr--r-- (or even=20 drw-r--r--) as there's no reason there should be execute permissions on=20 the directory. If anyone's curious, here's the file handling part of the code. Truly=20 nothing special. If anyone would like to see anything else, I'd be=20 happy to oblige. $docroot =3D '/Library/Webserver/Documents/'; $dir =3D 'temp/'; $path =3D $docroot.$dir; $id_string =3D uniqid('',1); $filename =3D 'apps-'.$id_string.'.txt'; $fh =3Dfopen($path.$filename,'w') or die($php_errormsg); fputs($fh,$app_string); fclose($fh) or die($php_errormsg); echo 'Here\'s your file! Download now!
'; echo ''.$filename.''; As this is the first time of done anything like this, I'd appreciate=20 any comments. Thanks! Michael