View Full Version : saving query result file to local drive

10-15-2004, 02:59 PM
I need to save a comma delimited file which contains the results of a Mysql database query to the users local drive. When I try to do the fopen I get "failed to open stream: Permission denied". I am new to PHP and I don't understand what permission I need to set and where. This is an internal intranet running on Linux with Apache. Maybe there is a better way to accomplish this.

10-16-2004, 03:49 AM
U need to set the file- and directorypermissions so that your PHP script can write the file. To do this, go to your shell as root (or another user with enough privileges) and execute command :

#>chmod 777 <path-to-directory>

Change the path to directory with the directory where u wanna write the file, for example /var/www/html/datadir.

10-18-2004, 01:50 PM
I probably didn't explain very well what I am trying to accomplish. I want to write the report to the client local drive (c drive on windows xp). The chmod 777 you suggested would be for a path/file on the Linux box which is the server. Maybe I need to write the file to the Linux box first and then access it a different way like FTP. Since PHP is able to write cookies to the client local drive I thought maybe there was a way to write files directly to the local drive. I also wanted to make it easy on the users since they would just want to press a button and get their report from the database query. Any suggestions on the best way to accomplish this would be appreciated. Thanks for the help.

10-18-2004, 04:35 PM
You could set the header of the PHP page to match the file you are downloading mime type and send the data you want.

10-18-2004, 04:45 PM
Sorry to be so dense but marek_mar could you please explain your post.

10-18-2004, 04:47 PM
i think there are 2 options:
- ftp is to the clientmachine (if he allows this)
- have the browser download it (either initiating an automatic download like marek_mar suggest, or show a downloadlink)

10-18-2004, 06:03 PM
Sorry to be so dense but marek_mar could you please explain your post.
That would be difficult... but if you want to spare me all the explaining it you can read the php manual on the header() (http://www.php.net/header) function. The last example on that page (not user submited) tels you how to do it with a generated pdf file.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum