6

I made a website, I probably didn't do it like I should have, but I was new to PHP at the time. So in order to save me lots of frustration of trying to re-write a script to display photos on my site, I need to run a *.php file, and make the output if it go into a var called "$html". I know it might sound strange, but that's what I need.

From inside index.php, I include photos.php; In photos.php, I need to declare $html with the output of a script called photos_page.php;

For example: $html = parse_my_script("../photos_page.php");

Thank you

Matt
  • 2,790
  • 6
  • 24
  • 34

10 Answers10

12

Answer: To do that, you can use PHP's Output buffering/control. Here's some simple function that gets script output and returns it:

Code:

Things used: ob_start() ob_get_clean() is_readable()

function getScriptOutput($path, $print = FALSE)
{
    ob_start();

    if( is_readable($path) && $path )
    {
        include $path;
    }
    else
    {
        return FALSE;
    }

    if( $print == FALSE )
        return ob_get_clean();
    else
        echo ob_get_clean();
}

Usage:

$path = '../photos_page.php';
$html = getScriptOutput($path);

if( $html === FALSE)
{
    # Action when fails
}
else
{
    echo $html;
}
Gabriel Nahmias
  • 920
  • 3
  • 15
  • 20
Robik
  • 6,047
  • 4
  • 31
  • 41
  • Thanks, your code works. however, *my* code is so not-cool, that I decided to just include() it from the main index.php file. – Matt May 03 '11 at 21:30
2

You can use output buffering. This will place all output, that would normally be sent to the client, into a buffer which you can then retrieve:

ob_start();
include '../photos_page.php';
$html = ob_get_contents();
ob_end_clean();

If you wish, you can place this functionality into a function to have it work as you described:

function parse_my_script($path)
{
    ob_start();
    include $path;
    $html = ob_get_contents();
    ob_end_clean();
    return $html;
}

This, of course, assumes that your included file doesn't require the use of global variables.

For more information, check out all the output control functions:

http://www.php.net/manual/en/ref.outcontrol.php

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • Hello! I am sorry, i didn't understand your question very well... I have the following PHP line: shell_exec("find ../ -name 'MY_FILE.pdf'") - I want to assign this output (Which returns a directory, with the location of the file) as a usable string to a var called $pdf_path ... I tried using your function but coul'nt figure out how to work with it... – Raul Chiarella Apr 19 '22 at 23:56
2

You'll want to try file_get_contents

$html = file_get_contents("http://www.yourwebsite.com/pages/photos_page.php");

//this will not work since it won't run through web server
//$html = file_get_contents("../photos_page.php");
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
2

This should do the trick:

ob_start();
require('../photos_page.php');
$html = ob_get_contents();
ob_end_clean();
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
1

You should use file_get_contents("http://yourdomain.com/path/to/photos_page.php") for that.


BUT: If I were you, I would do it in this way:

photos_page.php

<?php

function get_photos_html() {
    $html = // generate html
    return $html;
}

?>

main_file.php

<?php

include('../photos_page.php');

$html = get_photos_html();

?>
akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • 2
    Nope, he wants the script's *output*. –  May 03 '11 at 19:19
  • But _file_get_contents_ returns output! – akashivskyy May 03 '11 at 19:20
  • 1
    @Kashiv: If I'm not mistaken, this function returns output only if you are fetching the php page through a web server. `file_get_contents("../photos_page.php")` won't work. In this case, you will be retrieving the file locally. Needs something like `file_get_contents("http://www.domain.com/photos_page.php")` – Dutchie432 May 03 '11 at 19:21
  • This unnecessarily creates a web request which uses many more resources than just simply using an output buffer. – webbiedave May 03 '11 at 19:30
  • @delnan is right. I do want the output. However, the whole stupid variable thing was not working right. Maybe because I was laoding "A" [index.html] which loads "B" [photos.php] which loads "C" [photos_page.php], and ... well it's too complicated to explain, I've decided to work it another way. – Matt May 03 '11 at 21:32
1

You want to have a look at the method ob_start() and ob_flush(), ob_get_contents(), etc. http://us.php.net/manual/en/ref.outcontrol.php

This will allow you to output data from your PHP file into a specific variable.

You should think about rewriting though of course :)

So basically:

ob_start();
include('yourfile.php');
$html = ob_get_contents();
ob_end_clean();
jsgoupil
  • 3,788
  • 3
  • 38
  • 53
1

Output buffering will hold outputted text in memory instead of actually outputting it to the response.

First, create a buffer by calling the ob_start() function. When you are done storing output in the buffer, call ob_get_clean() to return the buffer contents and clear the buffer.

ob_start();
include "../photos_page.php";
$html = ob_get_clean();
Michael
  • 34,873
  • 17
  • 75
  • 109
0

I guess what you posted is pseudo code.

A code you wrote before and which didn't work out well shouldn't be used again. I would rewrite the parts you need and create some functions or even a class which you can use to get whatever you need.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

If your photos_page.php works like this:

<?php
    // very basic code
    $img = "/path/to/my/image.jpg";
    echo '<img src="' . $img . '">' . PHP_EOL;
?>

Then you could use:

$html = file_get_contents('http://mysite.com/photos_page.php');

But really, you should re-write the code so you don't have to do it this way.

drudge
  • 35,471
  • 7
  • 34
  • 45
0

You could also do this if $path returns a value:

$path = '/path/to/your/script.php';
$html = return include $path;
rayalois22
  • 161
  • 3
  • 7