user4061223
user4061223

Reputation:

How to retrieve all contents of a file from file_get_contents through PHP?

I am trying to work out how to retrieve all of the contents of a text file as a string and store it in a variable through PHP.

The code below works fine through file_get_contents and the PHP code is sent to the $get variable correctly, however, it doesn't get displayed on index.php correctly.

The index.php file will echo out hi rather than just hi.

index.php

<?php
    $display = "hello";
    $get = file_get_contents("get.txt");
    echo $get;
?>

get.txt

hi, $display;

How would I go about fixing this? I would really appreciate any help, thanks!

Upvotes: 1

Views: 515

Answers (2)

Michael Berger
Michael Berger

Reputation: 11

For some reason the webserver is not processing the PHP code. If you are working locally(from your computer) then you need server software running on that machine for PHP code (HTML, CSS, and Javascript will run fine). If that's the case get a program like "WAMP Server" and you'll be good to go with PHP.

Upvotes: 0

Dmitry Khaperets
Dmitry Khaperets

Reputation: 26

index.php:

<?php
   echo 'hi';
?>

some.php:

<?php
    require 'index.php';
?>

Will be shown text hi.

Upvotes: 1

Related Questions