Basic PHP help is needed

20 replies
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: welcome.txt
file's content:

Will this file with these
lines be opened by php code?

2) file's name: fileopen.php
file's content:

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

As far as I understand, the PHP script in "fileopen.php"
should open my "welcome.txt" for reading, right? However,
when I click on the the file's link, my browser shows the
full content of "fileopen.php" instead, as if it were an HTML
file. Why is it so?
#basic #needed #php
  • Profile picture of the author zoobie
    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?
    {{ DiscussionBoard.errors[290392].message }}
    • Originally Posted by zoobie View Post

      "is your sever got php support?"
      yes


      Originally Posted by zoobie View Post

      "...what php version is your server running on?"

      "I will suggest you use <?php phpinfo();?> and save is as test.php to see if your server caple of running php first..."
      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"
      {{ DiscussionBoard.errors[290500].message }}
  • Profile picture of the author Spencer Westwood
    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.
    {{ DiscussionBoard.errors[290870].message }}
    • Originally Posted by Spencer Westwood View Post

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

      Kind regards, Spencer.
      Thank you for your response, but, sadly, when I tried your script I got
      the same result - my browser shows the full content of "fileopen.php" 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.
      {{ DiscussionBoard.errors[291671].message }}
  • Profile picture of the author adambeazley
    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.
    {{ DiscussionBoard.errors[291452].message }}
    • Originally Posted by adambeazley View Post

      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.
      Thanks for letting me know, adambeazley. I'll be keeping it in
      my mind while creating scripts.
      {{ DiscussionBoard.errors[292941].message }}
  • Profile picture of the author Robert Plank
    Guys, you are all making this WAY too complicated.

    <html>
    <body>
    <?php
    echo file_get_contents("welcome.txt");
    ?>
    </body>
    </html>
    {{ DiscussionBoard.errors[291931].message }}
    • 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.
      {{ DiscussionBoard.errors[292175].message }}
  • Profile picture of the author Plinko
    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.
    {{ DiscussionBoard.errors[292385].message }}
    • Hello Plinko!!!

      Thanks for your invovement in my problem.

      Originally Posted by Plinko View Post

      ...If I read correct, you are saying the entire contents of your fileopen.php file are being displayed in the browser. Correct?
      Originally Posted by Plinko View Post

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

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

      Correct?
      Yes, that's correct.

      Originally Posted by Plinko View Post

      ...But I am confused why the phpinfo() test file you ran returned the actual server output of the phpinfo() function.
      I don't know, but that's exactly what it did.




      Originally Posted by Plinko View Post

      Let me ask, when you ran the phpinfo() test, did you run it from the same directory as fileopen.php?
      Well, originally phpinfo() was in the root derictory, and fileopen.php and

      welcome.txt were placed into another directory that I had created.

      However, after I read your post, I decided to delete that directory and
      move the files into the root directory. So now I don't have any other directories
      except for the root directory, and all three files (phpinfo(), fileopen.php,
      welcome.txt ) are in it.

      The result is still the same: phpinfo() works fine, but fileopen.php has problems.

      Originally Posted by Plinko View Post

      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).
      Where is this .htaccess file? I can't see it anywhere in the root directory

      (which is the only directory now).
      {{ DiscussionBoard.errors[292902].message }}
      • Profile picture of the author JamesFraze
        usually, if it's a linux server, files that start with a . are hidden. Check the settings in your ftp program that you can see the files that start with .

        If you have a shell account, you can login and issue the following command:
        ls -al

        to get a directory listing that shows all files.

        Can you provide the following:
        1. attach the code
        2. link to the phpinfo() file
        3. attach the .txt file too

        Be warned that letting others see the phpinfo is a security issue and can be used to break into your server, so don't leave it up indefinately.


        Also, what is the objective of what you are trying to do? Perhaps knowing what you want done, will allow someone to provide a better solution that what you originally started with.

        forgive me if I don't answer ... off to work
        {{ DiscussionBoard.errors[292974].message }}
        • Hello JamesFraze!

          Thank you for your response.

          Originally Posted by JamesFraze View Post

          usually, if it's a linux server, files that start with a . are hidden. Check the settings in your ftp program that you can see the files that start with .
          but I am not using any FTP program...

          Originally Posted by JamesFraze View Post

          If you have a shell account, you can login and issue the following command:
          Originally Posted by JamesFraze View Post

          ls -al to get a directory listing that shows all files.
          To tell you the truth, I don't even know what a shell account is,
          so I don't know whether my account is a shell one or not.

          Originally Posted by JamesFraze View Post

          Can you provide the following:
          1. attach the code
          2. link to the phpinfo() file
          3. attach the .txt file too
          I am afraid you think I am doing something big here, but I am
          just a newbie and what I am doing indeed is just learning how
          to use PHP. It just happened so that the first command that I
          tried to look at in PHP was fileopen, and it just happened so that
          it didn't work on my first PHP-supported server.

          So, the code is still the same:
          <html>
          <body>
          <?php
          $file=fopen("welcome.txt","r") or exit("Unable to open file!");
          ?>
          </body>
          </html>

          And the text in the text file is still the same, too (nothing less,
          nothing more) :

          Will this file with these
          lines be opened by php code?

          As for the phpinfo() file link, I can't type it in here fully as
          I haven't had 15 posts on this forum yet (such are the rules
          here), so I will type it here without the initial "http//:" part:

          insights.freehyperspace5.com/test.php

          Originally Posted by JamesFraze View Post

          Be warned that letting others see the phpinfo is a security issue and can be used to break into your server, so don't leave it up indefinitely.
          Thanks. I'll delete it as soon as my problem is solved.

          Originally Posted by JamesFraze View Post

          Also, what is the objective of what you are trying to do? Perhaps knowing what you want done will allow someone to provide a better solution that what you originally started with.
          The reason why I started learning PHP is the following: I want

          some web-pages' html code to be copied periodically and stored
          somewhere (for example, on some sever or on my computer). I
          could do this on my computer, but I can't do it all the time as I
          also often need to turn the computer off and leave for work.

          Thus, I was advised to "delegate" this job to some server, and for
          that I need to learn PHP. (A question about a PHP web-page copying
          command I have already asked in another thread.)
          {{ DiscussionBoard.errors[293195].message }}
  • Profile picture of the author Plinko
    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?
    {{ DiscussionBoard.errors[293297].message }}
    • Originally Posted by Plinko View Post

      Rename test.php to phpinfo.php
      rename fileopen.php to test.php
      try to access both files in your browser
      So, I renamed them and accessed them in my browser.
      Now phpinfo.php was showing me numerous tables with
      php information and test.php was still showing me its content.

      After that I had to rename them back because JamesFraze
      requested the link to the test.php file (see the post that is
      right before yours)

      Plinko, can you please access my files using your browser?
      Perhaps, it's my browser's problem. Here are the links
      (no www is needed):

      fileopen.php:
      insights.freehyperspace5.com/fileopen.php

      test.php:
      insights.freehyperspace5.com/test.php

      welcome.txt:
      insights.freehyperspace5.com/welcome.txt

      Originally Posted by Plinko View Post

      Also, is this your server that you have configured, or are you renting from another host?

      This one is a free php-supporting hosting. Here is its url in case you need to
      check it (please append com and prepend www ):

      .freehyperspace5.

      I have chosen this server to start out with my php learning
      and practicing. Perhaps, I should choose another server.
      {{ DiscussionBoard.errors[295157].message }}
      • Profile picture of the author floatingatoll
        Originally Posted by truckload-of-thoughts View Post

        So, I renamed them and accessed them in my browser.
        Now phpinfo.php was showing me numerous tables with
        php information and test.php was still showing me its content.
        Do both files have the same UNIX permissions? If the phpinfo.php file is marked executable, but the test.php file is not, you might try giving test.php those same permissions. This may not help but I expect it's rather harmless either way.

        Does the directory have a .htaccess file that mentions phpinfo.php but not test.php? If so, try duplicating whatever section is present for phpinfo.php for the second file and see if that helps. This is less likely to be the problem except in unusual cases.
        {{ DiscussionBoard.errors[295239].message }}
        • Hello floatingatoll,

          thank you for your response.
          Originally Posted by floatingatoll View Post

          Do both files have the same UNIX permissions?
          I don't think so, because I didn't specify any permission for

          ether file while I was creating them.

          Originally Posted by floatingatoll View Post

          Does the directory have a .htaccess file that mentions phpinfo.php but not test.php?
          I can't see the .htaccess file in the directory. I think its

          hidden.

          I don't see any reason why there should be some
          discrimination between these two files (unless their content
          may somehow matter). The file-creating procedure in
          either case was exactly the same.

          Originally Posted by floatingatoll View Post

          ...try duplicating whatever section is present for phpinfo.php for the second file and see if that helps. This is less likely to be the problem except in unusual cases.
          How do I perform this duplicating?
          {{ DiscussionBoard.errors[296551].message }}
  • Profile picture of the author Plinko
    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.
    {{ DiscussionBoard.errors[295293].message }}
    • Hello again Plinko!!!

      You were able to access my files in your browser?! So, what did
      you see when you opened the fileopen.php link? Did you see a
      blank page, the php code, or the contents of welcome.txt?

      What normally should be displayed there? I thought that the
      content of welcome.txt was supposed to be displayed, but,
      having re-read one of your earlier posts, I began to doubt:
      Originally Posted by Plinko View Post

      ...If it was working correctly, then your
      Originally Posted by Plinko View Post

      original example would display a blank page which you could
      "view source" and see only the HTML tags without PHP code.
      On the other hand, if it should be a blank page displayed,
      what's the point then in having the "read-only" mode in
      fopen() function? (What is there for me to read, if, having
      opened a file, I can only see a blank page?)



      Originally Posted by Plinko View Post

      Try this. Rewrite your code in notepad
      Originally Posted by Plinko View Post

      and upload it and try again and I think it should work just fine.
      I followed on what you said here, so here is a detailed
      step-by-step account of what I did:

      1) Firstly, I opened my file manager on the server and deleted
      fileopen.php and welcome.txt

      2) Then, created on my computer's desktop two notepad files
      (i.e. both are with .txt extension): fileopen.txt and welcome.txt ,
      the content in both files being absolutely the same as in
      those deleted.

      3) Uploaded both files to the server.

      4) Opened the link to welcome.txt in my web browser - what
      was seen on the screen was exactly what was written in the file.

      5) Opened welcome.txt in the account editor (clicked on the
      "edit the file" button) - what was seen in the main field of the
      editor was slightly different now from what was written in the file:
      there were no "line-breaks" in the text, so the whole content
      of the file looked as if it were originally one line.

      6) Then I opened the link to fileopen.txt in my web browser -
      strange enough, but I didn't see anything on the screen!

      7) Opened fileopen.txt in the account editor - the main filed of
      the editor didn't show anything either.

      8) Renamed fileopen.txt to fileopen.php.

      9) Opened it in my web browser - blank page (is it what it should
      be like?)

      10) Opened it in the editor - nothing in the field again!

      11) Typed the php code into the editor's main filed and saved it.

      12) Opened fileopen.php in the web browser - the result was as
      always: the whole content of the file (i.e. the php code) was
      displayed on the screen.

      13) Assuming that my desktop had a virus, I connected my laptop
      to the internet and performed all these steps again on the laptop -
      the results were just the same.

      What do you think about all of this?

      I seem to have gotten stuck in the very beginning.

      (Feel like somebody has put a spell on me so that I wouldn't
      succeed much in learning PHP )
      {{ DiscussionBoard.errors[296518].message }}
      • Profile picture of the author rwil02
        Originally Posted by truckload-of-thoughts View Post

        5) Opened welcome.txt in the account editor (clicked on the
        "edit the file" button) - what was seen in the main field of the
        editor was slightly different now from what was written in the file:
        there were no "line-breaks" in the text, so the whole content
        of the file looked as if it were originally one line.

        6) Then I opened the link to fileopen.txt in my web browser -
        strange enough, but I didn't see anything on the screen!
        This tells me that your "account editor" is operating as an HTML editor, rather than a text editor.

        So that is what will be causing your problem.

        see if there is an option in it somewhere for "text mode", "source mode" or similar.
        Signature

        Roger Willcocks
        L-Space Design
        Please vote to help me win a 3kW solar array

        {{ DiscussionBoard.errors[297060].message }}
        • okay, i've changed the server, and now everything is fine.
          The previous server's editor was a nightmare.
          My big thanks go to everyone who was helping me here,
          especially to Plinko
          {{ DiscussionBoard.errors[300080].message }}

Trending Topics