#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    I need to be able to write a large amount of text to a .php file using fopen/fwrite, however, when ever i try an enter..

    fwrite($file, "<? include("header.php") ?> ..etc..etc.");

    ..I get a parse error. How can I get a large amount of php script written to $file without getting a parse error?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Your one line script contains two errors which could be seen easily .
    First fwrite itself is a PHP function why do you again start PHP tags before the include file and the second is that you need include a file over file here. File must be included before the fwrite function and the string you have to write fron the include file is to be passed in fwrite function.

    ------------------
    Nikunj
    MYSQL/PHP/XML
    ** Expertise comes with experiece ** Nikunj
  4. #3
  5. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    fwrite($file, "<? include("header.php") ?> ..etc..etc.");
    >>


    you need to eascape from the double quotes..
    also i think you need to escape from the php deliminators.

    try some thing like..

    <?
    $variable="<? include("header.php") ?> ..etc..etc.";
    fwrite($file,$varaible);
    ?>


    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited September 08, 2000).]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    thanks for your help guys, however, it should be noted that I have alot more to insert than just <? include(""); ?> I have about 100-300 lines of php script and html I have to write to the file. Is fwrite(); the function I want to use? Is there a better way to write this file? I need to write it in this script because I have variables i'm submitting from a form on the previous page that need to be echoed in the php file i'm writing to.
  8. #5
  9. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    probably you need to use addslashes() function for escaping all the double quotes and other characters.

    <?
    $variable="<? include("header.php") ?> ..etc..etc.";
    $variable=addslashes($variable);
    fwrite($file,$varaible);
    ?>



    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."
  10. #6
  11. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    astrix,

    Write all the data you wish to write to the file to a variable first:

    $data='<? include("somefile.php");';
    $data.='somefunction("foo");';
    $data.='otherfunc($foo);';

    Also, notice I used single quotes in the assignment? This serves two purposes. First, it prevents PHP from evaluating code. In line 3, if you used double quotes it would replace $foo with the current value, but not what you want if you really want to write the string '$foo'. Second, most of your quotes that you'll write will be double quotes, so you cut down on the number of things you'll need to escape.

    Rod
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    I'm still getting a crappy parse error. I think its the <? ?>. I also tried htmlspecialchars(); any other ideas? lol they need a phpspecialchars();
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    i didn't read rod k's reply, i'll try that now
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    thank you very much rod k, that worked like a charm, however, I also need to be able to put variables in with the text located in $foo = '<? ?>' and have the variables echo their values written earlier in the script...ie...

    $foo = "hello world";
    $bar = '<? include("header.php"); ?> <html><head><title>$foo</title></head>';

    ...I sure hope theres a way I can do this. anybody help?
  18. #10
  19. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    OK, to do that you can close the quote and use the variable directly. Let's say you want to actually include the current value of $foo to the variable $bar in the file:

    $data='$bar='.$foo;
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Gig Harbor, Washington, USA
    Posts
    29
    Rep Power
    0
    Here are two solutions I came up with:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $fp = fopen("header.php", "r");
    $variable = fread($fp, filesize ("header.php"));
    fclose($fp);

    fwrite($file, $variable);
    [/code]

    and then the second,
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $variable = include("./header.php");
    fwrite($file, $variable);
    [/code]

    Not too sure if the second one will work, but I am sure the first one will. Hope this helps

    [b]EDIT[/b}: Ahh damnit, the first time I come up with some good solutions some already answered you Hehe, just noticed one of your replies.

    ------------------
    chris swanicke
    swanicke@defected.net

    [This message has been edited by swanicke (edited September 08, 2000).]
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Chicago, IL USA
    Posts
    48
    Rep Power
    18
    thanks everyone so much for your help, things are working great. proving once again that devshed rocks! i click on their ad banners just cuz' i like devshed so much lol.

Similar Threads

  1. fwrite user input, variable problem, "invalid conversion"
    By Hero Doug in forum C Programming
    Replies: 15
    Last Post: January 20th, 2004, 05:02 PM
  2. Zero Bytes - fwrite
    By LynneR in forum PHP Development
    Replies: 6
    Last Post: September 18th, 2003, 10:04 AM
  3. fwrite with include
    By RobCat in forum PHP Development
    Replies: 3
    Last Post: September 1st, 2003, 08:46 AM
  4. Having some troubles with sessions
    By watco?! in forum PHP Development
    Replies: 3
    Last Post: April 23rd, 2002, 11:32 PM
  5. include, fwrite bafflement with \'
    By PxlJuNkiE in forum PHP Development
    Replies: 5
    Last Post: September 27th, 2001, 06:24 PM

IMN logo majestic logo threadwatch logo seochat tools logo