1

I want to include the content of a file $source, which has php-code inside.

// using_source.php
$source = '../filepath/..';

<?php echo file_get_contents($source); ?>

source.php

<?php $multiplicator = 2; ?>
<?php echo 1 * $multiplicator; ?> Apples <br>

I get following:

Apples

I want:

2 Apples

Any approaches to solve this?

4
  • why not just include or require the source.php file so it's contents get parsed as php and not just a string. Commented Jan 19, 2016 at 9:46
  • 2
    Possible duplicate of How to run php code from file_get_contents or file in a function Commented Jan 19, 2016 at 9:46
  • the code above is surely not the actual code? what do you get from the file_get_contents call? Commented Jan 19, 2016 at 9:47
  • Use cURL to retrieve the file, run the PHP code and collect the output that's normally given back to the browser. Commented Aug 8 at 14:49

2 Answers 2

2

Instead of using file_get_contents(), use include():

<?php include($source); ?>

This will actually run the code through the PHP interpreter, rather than just printing its text-contents inside the page.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay. I just need to clean the url for include, because it is an absolute path. But yes, it works this way. Thanks
You can include an absolute path! :) If file_get_contents() can read it, so can include(). No problem!
I mixed up url with absolute path! But everythine is working fine now! Thanks!
1

I think you may want to take a different approach to your problem like other people have mentioned.

I'm going to presume your two files are in the same directory, if not you may need to alter the below slightly.

source.php

<?php

// The variable we want to work with
$multiplicator = 2;

index.php

<?php 

require 'source.php';

echo (1 * $multiplicator) . ' Apples';

I'm not exactly sure on what your use case is for this to be honest, it seems a little odd but the above should at least fix your issue.

The reason why your current code is not working is due to the fact file_get_contents() returns the content as a string, that string isn't parsed by PHP so you do not have access to any variables you declared in a file that you call by file_get_contents()

Hope that helps, and here's the DOCs for file_get_contents() and require: http://php.net/manual/en/function.file-get-contents.php

http://php.net/manual/en/function.require.php

1 Comment

Thank you for your detailled answer. But in my case I have hundreds of source files and the output in my index.php is dynamically generated. Not easy to understand, without seeing it, but I need the calculation in my source file. And my problem is already solved, thank you nevertheless!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.