View Full Version : PHP & Writing to Files
cmahnken
05-20-2001, 09:39 PM
I want to save some data from a form to a .csv file. Formatting the data is no problem, but getting fopen() to return a file handle does seem to be a bit of an issue.
This code doesn't seem to work:
[nbsp][nbsp]// $data already has the csv info to write
[nbsp][nbsp]$szFile = "storage/survey.csv";
[nbsp][nbsp]$hFile = fopen($szFile, 'a'); echo $f;
[nbsp][nbsp]$i = fwrite($hFile, $data, strlen($data));
[nbsp][nbsp]fclose($hFile);
Am I missing something obvious? Do I need to chmod the file that is doing the writing?
$szFile needs to be a fully qualified path:
$szFile = '/big/dom/xdomain/.../storage/survey.csv';
Rich
cmahnken
05-21-2001, 02:02 AM
That's actually the way it is in my code, I just remove the full path to protect the innocent. I'll put together a test case and try it out and get back to you if I still can't get it to work.
------------------
Chris Mahnken
cmahnken
05-22-2001, 01:33 AM
No joy. Even using the full path, with and without the file already existing, I can't seem to get this to work.
$data = "test data\n";
$file = "/big/dom/xcruisingco/www/mkt/dbfile/survey.csv";
if (file_exists($file))
{
[nbsp][nbsp] $f = fopen($file, "a");
[nbsp][nbsp] if ($f)
[nbsp][nbsp] {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]$x = fwrite($f, $data, strlen($data));
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]fclose($f);
[nbsp][nbsp] }
[nbsp][nbsp] else
[nbsp][nbsp] {
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]echo "Drat!";
[nbsp][nbsp] }
}
else
{
[nbsp][nbsp] echo "file missing";
}
Anybody have any ideas?
------------------
Chris Mahnken
(1) What happens/outputs when you run the script?
(2) What errors are displayed onscreen or in the error log?
Rich
Have you chmod'ed the file (0666 or 0777) before trying to write to it?
Dan
cmahnken
05-22-2001, 02:36 PM
Rich:
1) Nothing happens. In tests to create the file, no file is created and fopen() returns false. In tests to write to a file fopen() again fails. There are no onscreen errors, just the false return from fopen().
2) The php_error log shows:
PHP Warning:[nbsp][nbsp]fopen("/big/dom/xcruisingco/www/mkt/dbfile/survey.csv","a") - Permission denied in /big/dom/xcruisingco/www/mkt/survey2.php on line 51
Dan,
I'll try that now. Thanks for the hint.
------------------
Chris Mahnken
cmahnken
05-22-2001, 02:42 PM
Dan gets the prize. I knew it was something that my tiny microsoft addled brain just couldn't quite understand. Now, how do I create a new file (which I can chmod from PHP)...
------------------
Chris Mahnken
I generally use a variation of your code so as to see where it's choking:
if ( !($myFile = fopen("./$filename", "w")) ) {
[nbsp][nbsp][nbsp][nbsp] print("Error: $filename could not be written to.");
[nbsp][nbsp][nbsp][nbsp] exit;
}
If you are trying to create the file on the fly, as opposed to merely writing to an existing file, then I believe you will be butting heads with Safe Mode.[nbsp][nbsp]I think the workaround would be to run the Apache executable version of PHP.[nbsp][nbsp]There is info scattered throughout the forums on that topic.
Dan
p.s.[nbsp][nbsp]I haven't had luck chmod'ing a file through PHP...
[This message has been edited by dank (edited 05-22-01@2:37 pm)]
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.