Page 1 of 1
newly created files - writing to
Posted: Thu Jul 24, 2003 6:12 am
by mesz
I'm writing form variables to a directory as a newly created file ( and also to a specified, already created file - list.dat ).
Because I want the contents of the form written to this newly created file I have to specify it - $filename = "$thename"; - and PHP returns loads of errors as it is not yet able to find the file.
How do I get round this?
Code: Select all
<?PHP
if($page <= 0)
{
$page = 1;
}
$old_umask = umask(0);
$filename = "$thename";
if (!file_exists($filename)) {
touch($filename);
chmod($filename,0777);
}
$stuff = $news;
$fw = fopen($filename,"w+");
fwrite($fw,$stuff);
fclose($fw);
$fp = fopen('list.dat',"a+");
$line .= "|" . $HTTP_POST_VARS['thename'];
$line .= "\r\n";
fwrite($fp,$line );
fclose($fp);
?>
Getting all these errors;
Warning: touch() [function.touch]: Unable to create file because No such file or directory in /home/.sites/14/site467/web/test/testwrite.php on line 17
Warning: chmod() [function.chmod]: No such file or directory in /home/.sites/14/site467/web/test/testwrite.php on line 18
Warning: fwrite(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 22
Warning: fclose(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 23
Posted: Thu Jul 24, 2003 8:06 am
by qartis
It's possible that $filename is empty, so it's touching and chmodding nothing at all, and giving errors. Try explicitly setting $filename at the beginning of the file.
Posted: Thu Jul 24, 2003 8:34 am
by mesz
Cheers for the response...I am a little confused though...what do you mean by explicitly stating at the start of the code?
I thought this was what I had done?
Posted: Thu Jul 24, 2003 9:32 am
by DuFF
I had the same problem. The only reason you got all the errors is because touch didn't work in the first place. Therefore the rest of the errors were because you could not open/write to the file because it didn't exist.
To fix this I had to CHMOD the directory that the file was being created in. Then touch should create the file and hopefully you won't get anymore errors.
Posted: Thu Jul 24, 2003 9:45 am
by mesz
Cheers DuFF - unfortunately my directory already has correct chmod...
Posted: Thu Jul 24, 2003 10:13 am
by DuFF
Why did you set a variable to equal a variable:
Instead just do
Just a guess . . .
Posted: Thu Jul 24, 2003 10:15 am
by mesz
it is set to $thename so the contents are written to the newly created file rather than a pre-specified file...
Posted: Thu Jul 24, 2003 10:20 am
by DuFF
Well does $thename have an extension? I'm not sure if you're already doing this but you could have:
Code: Select all
<?php
$filename = $thename."dat";
?>
I'm not really sure of the problem now so I'm just guessing.
Posted: Thu Jul 24, 2003 10:46 am
by mesz
DuFF wrote:Well does $thename have an extension? I'm not sure if you're already doing this but you could have:
Code: Select all
<?php
$filename = $thename."dat";
?>
I'm not really sure of the problem now so I'm just guessing.
DuFF, I appreciate all the help...this is a real stinker.
Adding the file extension would not help though....
The files are created fine, the contents are posted to the created files perfectly...but when the page with the form is first called up the client is presented with a screen of error messages because as far as PHP is concerned the file that is about to be written to does not exist..yet..
It is this
yet bit that I cannot work around.
Posted: Thu Jul 24, 2003 11:02 am
by qartis
@touch($filename);
@chmod($filename,0777);
?
Posted: Thu Jul 24, 2003 11:07 am
by mesz
I have amended the code - it turned out touch was unneccessary - and I am getting a lot less errors ( see below ), but I have these two remaining ones that will not go!:
Code: Select all
<?PHP
if($page <= 0)
{
$page = 1;
}
$filename =($thename);
$fw = fopen($filename,"w+");
$line1 .= "|" . $HTTP_POST_VARS['news'];
$line1 .= "\r\n";
fwrite($fw,$line1);
fclose($fw);
$fp = fopen('list.dat',"a+");
$line .= "|" . $HTTP_POST_VARS['thename'];
$line .= "\r\n";
fwrite($fp,$line);
fclose($fp);
?>
Current errors:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 14
Warning: fclose(): supplied argument is not a valid stream resource in /home/.sites/14/site467/web/test/testwrite.php on line 15
Cheers for your continuing assistance.
Posted: Thu Jul 24, 2003 11:52 am
by pootergeist
try setting filename from server root
$filename = '/home/.sites/14/site467/web/test/' .$thename;
also note: it is probably a rather bad idea to write to the same folder as the executing script - I would advise using a subfolder and only chmodding that.
Posted: Sat Jul 26, 2003 3:52 am
by mesz
Helllo pootergeist.
Sorry, I've been well rude.
Your idea worked... I just was so busy doing things I forgot to thank you.
One slight amendment was needed though -
instead of
pootergeist wrote:try setting filename from server root
$filename = '/home/.sites/14/site467/web/test/' .$thename;
also note: it is probably a rather bad idea to write to the same folder as the executing script - I would advise using a subfolder and only chmodding that.
I needed to write the extra dot after the $thename variable:
Code: Select all
$filename='/home/.sites/14/site467/web/test/test1/'.$thename .'.dat';
n.b. the .dat ( or whatever ) file extension was not necessary for the script to execute.
Cheers