Jump to content

[SOLVED] Writing to file


nesiak

Recommended Posts

this is the code i use to write to the beginning of a file! it reports an error at the line 6 what am i doing wrong ?

 

error

Parse error: parse error, unexpected T_VARIABLE in /home/www/nenadspp.freehostia.com/php/admin/writetofile.php on line 6

 

 

Code:

<?php 
function writetofile()
{
$file=fopen("posts.php","r+");
$title = $_GET["title"]
$text = $_GET["text"]
$writte = "<h2>$title</h2> <br /> $text"
fwrite (file$,$writte)
}
?>

Link to comment
Share on other sites

First off, you are missing your semi-colons (;) after each line except one. I'm not all that familiar with file writing in PHP, so I can't help you with the rest.

 

<?php 
function writetofile()
{
$file=fopen("posts.php","are+");
$title = $_GET["title"];
$text = $_GET["text"];
$writte = "<h2>$title</h2> <br /> $text";
fwrite ($file,$writte); //I think you were meaning to put the '$' at the beginning of the "file"...
}
?>

Link to comment
Share on other sites

The problem was that i never called the function (i had this error twice in one day!)

 

Now another error came to notice

 

Warning: fopen(posts.php): failed to open stream: HTTP wrapper does not support writeable connections. in /home/www/nenadspp.freehostia.com/php/admin/writetofile.php on line 4

Warning: fwrite(): supplied argument is not a valid stream resource in /home/www/nenadspp.freehostia.com/php/admin/writetofile.php on line 8

 

I'm stumped

Link to comment
Share on other sites

If you want to write to the beginning, shouldn't you be using 'w+' or 'x+'?

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

Link to comment
Share on other sites

No R+ is read/write, w+ would delete the contents of the file before writing to it , i need to preserve the file contents that's why i'm using r+

 

Fixed the problem, i was trying to write to a url and that is not possible !

Link to comment
Share on other sites

<?php 
function writetofile()
{
chmod("posts.php",766);
$file=fopen("posts.php","w+");
$title = $_GET["title"];
$text = $_GET["text"];
$writte = "<h2>$title</h2> <br /> $text";
fwrite ($file,$writte); //I think you were meaning to put the '$' at the beginning of the "file"...
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.