Write to .txt file

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
tetuanrp
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 1:20 pm

Write to .txt file

Post by tetuanrp »

I have just started using php on my mac os x and I'm going through a tutorial in a book using php with flash. I'm on the first excersize and having trouble already. Think it could be a configuration issue but thought I'd see if anyone could see a simple fix. Here is the php code that I'm using, which the book has given me.

Code: Select all

<?php
foreach ($_POST as $key=>$value) {
  $received .= "$key = $value\r\n";
  }

$printout = fopen("variables.txt", "w");
fwrite($printout, $received);
fclose($printout);
?>
When I run it I get this:
Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
Warning: Unknown: Failed opening '/Users/Admin/Sites/phpflash/ch02/feedback.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in Unknown on line 0

Is there something wrong with the file location and is that why its not creating a .txt file?

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Most likely you have not set the proper permission through chmoding.. :arrow: chmod()
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Try

Code: Select all

fopen("variables.txt", "w+")
tetuanrp
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 1:20 pm

Post by tetuanrp »

Hmm. Didn't seem to work. I put it up on my hosts server which seems to run my scripts fine, and it spits back "sent=OK" now. So it seems like its processing fine but it still doesnt create a .txt file. I set the chmod permissions to 755 on the folder that the php file is in as well.
tetuanrp
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 1:20 pm

Post by tetuanrp »

So I've got it working locally now, although on my remote server its not creating the file. Is setting the folder to 755 the correct permissions? (these are probably rookie questions, as that's exactly what I am!)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Code: Select all

if($printout = fopen("variables.txt", "w") and fwrite($printout, $received) and fclose($printout){
	print 'I created and wrote to variables.txt.';
}else{
	 print 'I either could not create or write to variables.txt.';
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

tetuanrp wrote:So I've got it working locally now, although on my remote server its not creating the file. Is setting the folder to 755 the correct permissions? (these are probably rookie questions, as that's exactly what I am!)
What do you think 755 ( drwxr-xr-x) does on a directory?

Does the webserver need read access on the directory to write into a directory? No.
Does he need write access? Yes.
Does he need execute/access to the directory? Yes.

Thus: you need -wx for the webserver..
Post Reply