Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2011, 04:34 PM   PM User | #1
sunnynosid
New Coder

 
Join Date: Sep 2011
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
sunnynosid is an unknown quantity at this point
Exclamation fwrite problem

My code:

<?
$file=fopen("/home/aaskacla/public_html/Aaskablogs/Blogs/$Blogtitle.php", "w+");
fwrite("/home/aaskacla/public_html/Aaskablogs/Blogs/$Blogtitle.php", "Hello, how are you?");
?>

Error coming:

Warning: fwrite(): supplied argument is not a valid stream resource in /home/aaskacla/public_html/Aaskablogs/Createblog/createblogscript.php on line 13

Please help me. Whats the problem?
sunnynosid is offline   Reply With Quote
Old 09-24-2011, 04:51 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
First of all, you should never be writing to a PHP script.
You are allowing someone to write PHP coding into your website.
What if I wrote some PHP scripting to delete all of your files?

You write to either text files, or a database of some kind. You
write content that will be put into a web page (or script).

Here is a correct example:

$data="This is a paragraph of the content that I want to write into the file";
$myFile = "mycontent.txt";
$file = fopen($myFile, 'w+') or die("can't open file");
fwrite($file, $data);
fclose($file);

In most cases, you will be appending to a file, not overwriting a file.

The difference is this:
$file = fopen($myFile, 'a+') or die("can't open file");

The plus (+) sign means to create the file if it doesn't already exist.
Also, any files must have the proper CHMOD permissions to write to them.

Any data written is sanitized .. all PHP and HTML removed.
Then, you include that file on your website.

<?php include("mycontent.txt");?>

The data file will be inserted where it is included.



.

Last edited by mlseim; 09-24-2011 at 04:54 PM..
mlseim is offline   Reply With Quote
Old 09-24-2011, 05:51 PM   PM User | #3
chris0
New Coder

 
Join Date: Sep 2011
Posts: 45
Thanks: 1
Thanked 6 Times in 6 Posts
chris0 is an unknown quantity at this point
the error

Quote:
Warning: fwrite(): supplied argument is not a valid stream resource ,
because you supplied a string as the 1st variable to fwrite, it should be


<?
$file=fopen("/home/aaskacla/public_html/Aaskablogs/Blogs/$Blogtitle.php", "w+");
fwrite($file, "Hello, how are you?");
?>
chris0 is offline   Reply With Quote
Users who have thanked chris0 for this post:
sunnynosid (09-25-2011)
Old 09-25-2011, 03:59 AM   PM User | #4
sunnynosid
New Coder

 
Join Date: Sep 2011
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
sunnynosid is an unknown quantity at this point
Thumbs up

Thanks a lot Chris. Your answer was very helpful for me.
sunnynosid is offline   Reply With Quote
Reply

Bookmarks

Tags
error, fwrite, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:26 AM.


Advertisement
Log in to turn off these ads.