Professional Web Applications Themes

How to echo content of a file? - PHP Development

Hello, I have a file that looks like this: ========= <td valign='top'>Data</td> <td><input type='text' name='Data' value="$_REQUEST['Data']" size='50'> </td> ========= I would like to include this in my script, but I want the $_REQUEST to be automatically filled. When I do: include 'file'; I have this printed to output AS-IS. I would like to treat file as string for echo (smth like:) $x=include 'file'; echo $x; However it doesn't work. Regards, Talthen...

  1. #1

    Default How to echo content of a file?

    Hello,
    I have a file that looks like this:
    =========
    <td valign='top'>Data</td>
    <td><input type='text' name='Data' value="$_REQUEST['Data']" size='50'>
    </td>
    =========
    I would like to include this in my script, but I want the $_REQUEST to be
    automatically filled.
    When I do:
    include 'file';
    I have this printed to output AS-IS.

    I would like to treat file as string for echo (smth like:)
    $x=include 'file';
    echo $x;
    However it doesn't work.

    Regards,
    Talthen


    Guest

  2. #2

    Default Re: How to echo content of a file?

    <pl> kirjoitti
    viestissä:df52e5$m6q$news.tpi.pl... 


    $x = file_get_contents('file');
    echo $x;
    // note: file_get_contents requires quite new version of php, it was added
    quite recently. although I can't remeber in which version it was added.

    or

    $x = implode('', file('file'));
    echo $x;

    or

    ob_start();
    readfile('file');
    $x = ob_get_clean();
    echo $x;

    I can think of some more, but those would be the easiest. In the examples
    above, if the file contains php, it will not be pd and executed. Now if
    you happen to have a file with php content and want it to be executed, then
    you do this:

    ob_start();
    include('file');
    $x = ob_get_clean();
    echo $x;



    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>
    Kimmo Laine <com>


    Kimmo Guest

  3. #3

    Default Re: How to echo content of a file?

    "Kimmo Laine" <com>
     

    Well.. it doesn't work. I changed it to:
    <?
    $hej=5;
    ob_start();
    include('file');
    $x = ob_get_clean();
    ob_end_clean();
    echo $x;
    ?>

    But still it shows me non pd "file".

    Regards,
    Talthen


    Guest

  4. #4

    Default Re: How to echo content of a file?

    "Kimmo Laine" <com> wrote:
     


    hmm it was file_put_contents I thaught that was rather recent, I
    needed to get the pear implementation for that on my server.

    cu

    mgk
    --
    "I never make predictions, I never have, and I never will" - T.Blair
    Markus Guest

  5. #5

    Default Re: How to echo content of a file?

    <pl> wrote in
    news:df52e5$m6q$news.tpi.pl (in part):
     

    That's because your file is HTML, not PHP. Do something like the
    following as the include file:

    <?
    echo '<td valign="top">Data</td>'."\n";
    echo '<td><input type="text" name="Data" value="' . $_REQUEST['Data'] .
    '" size="50"> </td>' , "\n";
    ?>

    Or

    <td valign="top">Data</td>
    <td><input type="text" name="Data" value="<? echo $_REQUEST['Data'] ?>"
    size="50"> </td>

    Ken
    Ken Guest

  6. #6

    Default Re: How to echo content of a file?

    <pl> kirjoitti
    viestissä:df55ub$9jl$news.tpi.pl... 
    >
    > Well.. it doesn't work. I changed it to:
    > <?
    > $hej=5;
    > ob_start();
    > include('file');
    > $x = ob_get_clean();
    > ob_end_clean();
    > echo $x;
    > ?>
    >
    > But still it shows me non pd "file".
    >[/ref]


    Well then you have to start thinking why include does not p the file.
    For example: maybe it hasn't got the <?php ?> tags around it? I can't think
    of that many reasons why include wouldn't p a file, but if it does, and
    when it does, that's how you get the output of it. Just find out why parsing
    fails.

    --
    SETI Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.berkeley.edu/>
    Kimmo Laine <com>


    Kimmo Guest

  7. #7

    Default Re: How to echo content of a file?

    "Ken Robinson" <com> 
    You are right :)
     

    But is there a way to treat HTML file like PHP one? :>

    Regards,
    Talthen


    Guest

  8. #8

    Default Re: How to echo content of a file?

    pl wrote:
     
    >
    > Well.. it doesn't work. I changed it to:
    > <?
    > $hej=5;
    > ob_start();
    > include('file');
    > $x = ob_get_clean();
    > ob_end_clean();
    > echo $x;
    > ?>
    >
    > But still it shows me non pd "file".
    >[/ref]

    If you REALLY can't change the included file to use proper PHP syntax, you
    could try using eval() but I suspect you really just need to add <?= ?>
    tags around the PHP variable. Make sure you understand the security
    implications before using eval() on a production machine, - you're leaving
    your system wide open to PHP injection. Even if you just fix the tags, your
    going to be vulnerable to XSS attacks.

    C.
    Colin Guest

  9. #9

    Default Re: How to echo content of a file?

    "Colin McKinnon" <mms3.com> 

    Ok, thanks. I already changed the file to a real php file, but I wanted to
    know if there is a way to treat plain string as php code :)
    Thanks.

    Regards,
    Talthen


    Guest

  10. #10

    Default Re: How to echo content of a file?

    >> Do something like the 
    >
    > But is there a way to treat HTML file like PHP one? :>[/ref]


    If you do "include" (or "include_once" or "require") then any file included
    this way is treated as PHP file, so if the file looks like Ken described it
    (my modification which works also if short tags are turned off and
    if $_REQUEST['Data'] contains double quotes or any other HTML special chars):

    <td valign="top">Data</td>
    <td><input type="text" name="Data"
    value="<?php echo htmlspecialchars( $_REQUEST['Data'] ); ?>" size="50"></td>


    then this should work as you wanted.



    Hilarion
    Hilarion Guest

Similar Threads

  1. how can I write what I echo to a new file?
    By Wayne... in forum PHP Development
    Replies: 1
    Last Post: July 7th, 03:51 PM
  2. php echo in php-only file
    By Thomas in forum PHP Development
    Replies: 3
    Last Post: November 14th, 12:51 AM
  3. [PHP] Read a file, extract and echo some information
    By Chris in forum PHP Development
    Replies: 2
    Last Post: October 21st, 05:14 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139