How to create a new text 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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

How to create a new text file?

Post by slipstream »

I am trying to create a text file using this:

$file2=fopen("hello.txt", "a+");
fwrite($file2, "whatever");
fclose($file2);

It never works?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: How to create a new text file?

Post by twigletmac »

slipstream wrote:It never works?
Do you get any error messages? Does anything happen?

Mac
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

actually I am doing 2 things here:

First I write to a specific file:
$docketEntry=$docketNum;
$file=fopen("docket.txt", "a+");
fwrite($file, "\n".$docketEntry);
fclose($file);

then right after I try creating this file like this:
$file2=fopen("hello.txt", "a+");
fwrite($file2, "test");
fclose($file2);

It's very wierd, it writes the value "test" to the first file even though I specify $file2?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Does the $file2 open? You can try to eliminate the problem by doing something like...

Code: Select all

<?php
if (!($file2=fopen("hello.txt","a+")))
{
echo("Could not open file");
}
?>
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

Yes the file is not opening, anyone know why?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', TRUE); // debug only
$docketEntry=$docketNum;
$file=fopen("docket.txt", "a+");
fwrite($file, "\n".$docketEntry);
fclose($file);

//then right after I try creating this file like this:
$file2=fopen("hello.txt", "a+");
fwrite($file2, "test");
fclose($file2);
or (if possible) take a look at the messages in the webserver's error-log. It might tell you more than we can guess ;)
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

ok here are my errors

Warning: fopen(hello.txt) [function.fopen]: failed to create stream: Permission denied in /mnt/home/www/mysite.com/www/docket/updateDocket.php on line 11

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/home/www/mysite.com/www/docket/updateDocket.php on line 12

Warning: fclose(): supplied argument is not a valid stream resource in /mnt/home/www/mysite.com/www/docket/updateDocket.php on line 13


What does it mean?
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

You don't have permission to open the file requested, chown it to 777.

Regards,
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

man am I dumb :roll:

Thanks guys, the files were 777 but the folder wasn't. Whew..
Post Reply