Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: fwrite problem

  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,470
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    the error

    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?");
    ?>

  • Users who have thanked chris0 for this post:

    sunnynosid (09-25-2011)

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks a lot Chris. Your answer was very helpful for me.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •