newly created files - writing to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

newly created files - writing to

Post 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
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Cheers DuFF - unfortunately my directory already has correct chmod...
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Why did you set a variable to equal a variable:

Code: Select all

<?php
$filename = "$thename";
?>
Instead just do

Code: Select all

<?php
$filename = "file.dat";
?>
Just a guess . . .
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

it is set to $thename so the contents are written to the newly created file rather than a pre-specified file...
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

@touch($filename);
@chmod($filename,0777);

?
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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
.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post 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
Post Reply