Basic PHP help is needed

by 20 replies
23
Hello, everybody!!!

I am sorry I am just a newbie, so my question will
sound probably silly to you.

I've just created a new account on one PHP-supporting
server, created my SQL database there, and uploaded
into the same directory these two files:

1) file's name:
file's content:




2) file's name:
file's content:









As far as I understand, the PHP script in ""
should open my "" for reading, right? However,
when I click on the the file's link, my browser shows the
full content of "" instead, as if it were an HTML
file. Why is it so?
#programming #basic #needed #php
  • is your sever got php support? I will suggest you use <?php phpinfo();?> and save is as test.php to see if your server caple of running php first.
    and what php version is your server running on?
    • [1] reply
    • yes


      I did everything according to what you told me, and, as a result,
      I got a web-page containing a huge number of tables of different
      kinds showing some information about the system, I guess.

      According to that page's header the version being used is
      "PHP Version 5.1.6"
  • Use file()..


    <?php

    $lines = file('Welcome.txt');
    foreach ($lines as $line_num => $line)
    {
    echo $line."<br />\n";
    }

    ?>


    I think that should do it... but test it anyway.

    Kind regards, Spencer.
    • [ 1 ] Thanks
    • [1] reply
    • Thank you for your response, but, sadly, when I tried your script I got
      the same result - my browser shows the full content of "" file,
      which now contains your script. Why is that so? I think it's not about
      the script that I am using, but rather something about the settings on
      my server.
  • I would highly recommend against using fopen or file as it really opens your system up to hackers. Basically they can exploit fopen to open a "BAD" script to gain access to your server or crash it all together.

    Your best bet is to setup a mySQL table and enter your text there.
    • [1] reply
    • Thanks for letting me know, adambeazley. I'll be keeping it in
      my mind while creating scripts.
  • Guys, you are all making this WAY too complicated.

    <html>
    <body>
    <?php
    echo file_get_contents("welcome.txt");
    ?>
    </body>
    </html>
    • [ 1 ] Thanks
    • [1] reply
    • Hello, Robert!!!

      Thank you for your response and the script you've provided.
      I am getting more and more convinced now that this is not
      the script's issue, but the server's, because even with your
      script I got the same results.

      I am going to try a different server.
  • Your initial description of the problem was confusing.

    If I read correct, you are saying the entire contents of your fileopen.php file are being displayed in the browser. Correct?

    That is, when you attempt to open fileopen.php in your browser you literally see:

    <html>
    <body>
    <?php
    $file=fopen("welcome.txt","r") or exit("Unable to open file!");
    ?>
    </body>
    </html>

    Correct?

    If so, this should be a server configuration issue. But I am confused why the phpinfo() test file you ran returned the actual server output of the phpinfo() function.

    The server has a directive in it which tells it to take .php files and run them through the PHP engine before outputting them. If it doesn't go through the engine then it usually just pipes the thing out as if it were a text file and you'll see the contents of the file instead of the php result.

    If it was working correctly, then your original example would display a blank page which you could "view source" and see only the HTML tags without PHP code.

    First thing I would check for is a .htaccess file in your html root OR any directory above the one this script is in (yes, it checks all dirs above the one your script it in).

    Let me ask, when you ran the phpinfo() test, did you run it from the same directory as fileopen.php? I still think this is server config issue but I'm confused as to where it could be if one php file works and the other doesn't.
    • [1] reply
  • Hi again truck,

    Can you try something for me? Rename the fileopen.php file to something we know works. Here's an idea.

    Rename test.php to phpinfo.php
    rename fileopen.php to test.php
    try to access both files in your browser

    Also, is this your server that you have configured, or are you renting from another host?
    • [1] reply
  • I can access your files, and we've found the answer.

    This is something common and I've seen it stump tech support and a developer when I worked in hosting.

    When you view the source of your page you'll find it isn't what you see when you view it in your browser.

    Nope, the source has converted items to their html entities. So instead of that <?php you'll see &lt;?php

    So my guess is you are using an HTML editor to write your code in the WYSIWYG mode.

    Try this. Rewrite your code in notepad and upload it and try again and I think it should work just fine.
    • [1] reply
  • [DELETED]

Next Topics on Trending Feed