FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Open, Write to and Close a txt file using php





ninjakannon
I'm running some PHP code, but I just can't get it to work. It's not even complicated Shocked

I created a simple PHP file to test out the code that didn't work, here it is:
Code:
<?php

$var1 = '4';
$var2 = '5';
$var3 = '6';

$filepath = 'writeThis.txt';
$writeText = '$var1 = ' . $var1 . "\n" . '$var2 = ' . $var2 . "\n" . '$var3 = ' . $var3;

$Open = fopen($Filepath,"w+");
fwrite($Open, $writeText, 100);
fclose($Open);

?>

And when I run the file I get the following:
Quote:

Warning: fwrite(): supplied argument is not a valid stream resource in previous_path/public_html/open_write_close.php on line 11

Warning: fclose(): supplied argument is not a valid stream resource in previous_path/public_html/open_write_close.php on line 12

I've tried setin the permissions on the files to 755, then even 777. But nothing changes.

What am I doing wrong?
Thanks for any help!
wumingsden
Code:
<?php

$var1 = '4';
$var2 = '5';
$var3 = '6';

$filepath = 'writeThis.txt';
$writeText = '$var1 = ' . $var1 . "\n" . '$var2 = ' . $var2 . "\n" . '$var3 = ' . $var3;

$Open = fopen($Filepath,"w+");
fwrite($Open, $writeText, 100);
fclose($Open);

?>


try instead...

Code:
<?php

$var1 = '4';
$var2 = '5';
$var3 = '6';

$filepath = "writeThis.txt";
$writeText = '$var1 = ' . $var1 . "\n" . '$var2 = ' . $var2 . "\n" . '$var3 = ' . $var3;

$Open = fopen($Filepath,'w+');
fwrite($Open, $writeText, 100);
fclose($Open);

?>
hexkid
ninjakannon wrote:
I'm running some PHP code, but I just can't get it to work. It's not even complicated Shocked

[...]

What am I doing wrong?
Thanks for any help!


Well ... you're missing three things.

The first and more important is to know why things fail.
Add these two lines to every script you're developing, right after the first <?php tag
Code:
error_reporting(E_ALL);
ini_set('display_errors', '1');


Then you missed a test for the result of the fopen() call.
Code:
// ...
$Open = fopen($Filepath,"w+");
if (!$Open) {
  exit('Unable to open the file for writing.');
}
// ...


and finally you are opening a file from a variable that didn't exist previously
$filepath and $Filepath are two different variables
Code:
// ...
$filepath = 'writeThis.txt';
// ...
fopen($Filepath, 'w+');
// ...



Now, don't just correct the third and less important mistake, correct them all.
ninjakannon
Thanks a lot.

I didn't know about the first bit of code that you sent gave me, that's really useful Wink

And I hadn't thought to put the 2nd bit of code, once again thanks.

The final thing... yes, I made a very big mistake Embarassed... Mis-spelling a variable Rolling Eyes

I've added in the code you suggested and changed the variable $filepath to $Filepath.

Thanks a lot Very Happy
Related topics
A very good PHP MySQL Tutorial
Creating a .txt file
.txt file to .php Lined to tables
how to compress a file using php ?
Startpages? permission?
Displaying .txt file on web page
I can't open directly .txt file on my desktop
Click adress?
VB 6.0 need help loading a .txt file into a text box
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
Notepad++
Can I add tasks to Windows Scheduled Tasks - Using PHP?
Help displaying pdfs using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.