Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP I can’t read a file in a browser

Kaworu

Active Coder
Hi!

I have a problem with my PHP code. The other PHP file I have takes info from some data (about orders in a web-shop) and writes it in a text file – this is working fine. What I wanna achieve with this code is to read it in my browser window, so people who will visit this site can have an easy access to this data.

The txt file is in the “orders – ok” folder and I am sure it is saved correctly. However, when I run this script in my browser, I see literally nothing – no data from this file, no messages of error, nothing. The place PHP script should “print” this data is perfectly empty.

Any help? ;-)

PHP:
<?php
    
    $document_root = $_SERVER['DOCUMENT_ROOT'];

?>

<!DOCTYPE html>

<html>

    <head>
        <title>Car parts - see all orders</title>
    </head>

    <body>
        <h1>Car parts - see all orders</h1>
        <h2>Clients' orders</h2>
        
        <?php
        
            $wp = fopen("$document_root/orders - OK/orders.txt", "rb");
            flock($wp, LOCK_SH); //blocking the file for reading
            
            if ($wp == false)
            {
                echo '<p><b>No orders.<br>
                    Please, try later.</b></p>';
                exit;
            }
            
            while (!feof($wp)) //read for the end of file
            {
                $order = fgets($wp);
                echo htmlspecialchars($order).'<br>';
            }
            
            flock($wp, LOCK_UN); //no file blockage anymore
            fclose($wp);
            
        ?>
    </body>

</html>

No data shown PHP.png
 
Can you show us an example of what's in the text file?
That way we can replicate your code to test it out.

What happens when you just read and echo the entire file instead of line by line?
Can you convert the while to not include !feof() and then inside the while put if(!feof($wp)){ echo 'end of file'; } else { ... }
These are two things you can try to debug your code yourself :)
 
What you gave is incomplete. But first thing that comes to mind Is this file saved as .php or .html?
I see no echo command for the <body> <h1>Car parts - see all orders</h1> <h2>Clients' orders</h2>
 
Hi people! Thanks for your input.

The text file I wanna read is as follows:

H:i, jS F Y\t1 tires \t2 oil cans \t3 spark plugs \t161.04 PLN \nH:i, jS F Y\t1 tires \t2 oil cans \t3 spark plugs \t161.04 PLN \nH:i, jS F Y\t1 tires \t2 oil cans \t3 spark plugs \t161.04 PLN \nH:i, jS F Y\t1 tires \t2 oil cans \t3 spark plugs \t161.04 PLNKowary Ul. 1-go Maja 1/1 \n18:42, 16th July 2021\t1 tires \t2 oil cans \t3 spark plugs \t161.04 PLNKowary Ul. 1-go Maja 1/1 \n19:14, 16th July 2021\t2 tires \t3 oil cans \t4 spark plugs \t300.12 PLNDanków \n

As to echo, I just created a "static" parts of the file with HTML code (I think it is easier to read?) and "dynamic" parts in PHP, so there is a clear distinction between those 2 things...? Is that problematic? (I hope not...).

The file was saved as PHP. I just got to copy and change it into html and... it is working? Actually I have some problems, but at least the code is being executed, even if improperly. So in future I should save as HTML, not PHP, right? Thank you ;-)

EDIT: Okay, I still got some problems. If you could help me, I would be grateful.

I dunno why, but it seems that the code is unable to read the txt file and shows the message that there is nothing inside (when there clearly is some data).

What's more, it does not seem to read the exit order and prints everything that's after that - not as an executable instruction, but just text "message". It even prints my PHP comments. I suppose I have some kind of a bug, but it is hard to me to tell where I made an error.

PHP-does not read data, shows sourcecode.png
 
Back
Top Bottom