RE: Problems with file_get_contents() and local PHP file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



D'oh!

The solution is so simple and clean that it almost hurts. I didn't see it in
the first approach!

There is a way to go all this, without using file() or file_get_contents()!
Cuz this would require to use a URL wrapper to retrieve compiled code, which
would cost overhead on the local webserver and the internet deamon. 

Just use and modify this code. You may also put it into a function;


// I strongly recommend to use this INSIDE a function to get a clear
namespace
function get_img_script($parameters)
{

	// backup $_GET variable
	$getbuffer = $_GET; 
	unset($_GET);

	$_GET = $parameters;

	ob_start();
	include 'yourscript.php';
	$buffer = ob_get_contents();
	Ob_end_clean();

	// restore $_GET variable
	unset($_GET);
	$_GET = $getbuffer;

	return $buffer;
}

And call it like this:

// define variables here. If you'd passed it with ?var=val
// use

$param = array('var'=>'val');
$img = get_img_script($param);


Hope that solves the problem in a clean way! 

Jan


-----Original Message-----
From: Richard Lynch [mailto:ceo@xxxxxxxxx] 
Sent: Wednesday, August 08, 2007 3:51 AM
To: Mike
Cc: php-general@xxxxxxxxxxxxx
Subject: Re:  Problems with file_get_contents() and local PHP file

On Sun, August 5, 2007 1:37 am, Mike wrote:
> Hey. My server is running PHP 4(Not actually my server so I don't know
> the exact version) and I'm having trouble with getting an image from a
> PHP file.

Use <?php phpinfo();?> to find out exactly what you've got.

> The problem is that originally this system was developed to be used
> with PHP 5, and used fopen() and stream_get_contents() to retrieve the
> image file from flash_frames.php. I thought I could just replace both
> of those with file_get_contents(), but it appears to fail when doing
> so. A quick test confirmed that file_get_contents(), when used
> locally, returns the PHP source as opposed to the output of the file.

If it's old enough, even file_get_contents may not be defined...

It first appeared in PHP 4.3.0, so if you're running something earlier
than that, upgrade.

Or I guess you could use:
implode('', file($filename));

> I didn't originally code this (I am but an inheritor who is learning
> PHP as he goes :P), so I'm at a loss for what should be done to fix
> this. I considered just converting the file into a function that
> returns the image, but I cannot find out how to return an image (Or
> convert the image to a string of bytes as the original code expected
> it to be).


> Any help is greatly appreciated. :)
>
> -Mike
>
> Original code for retrieving image:
>
> //Replaces res_viewer to flash_frames since they both take the same
> arguments
> $img=rawurldecode($img);
> $fl_imgsrc=str_replace('/res_viewer.php?','/flash_frames.php?scale='.
> $scale.'&',rawurldecode($img));

This is doing rawurldecode() on $img twice, which is almost for sure
not right.

It probably "works" for all the actual inputs you are using, but it
would fail if somebody passed in a filename that happened to have
"%xx" in the actual filename, almost for sure.

> //Grabs the generated image set up for the flash preview
> $handle = fopen($fl_imgsrc, "rb");
> $contents = stream_get_contents($handle);
> $fl_map = new SWFBitmap($contents);

This is using the Ming extension.

There is a Ming mailing list that may be helpful if it turns out that
$contents is fine, but the new SWFBitmap is "not working"

The number of Ming users is very tiny compared to PHP in general, and
I suspect not many hard-core Ming users are on this list.

> fclose($handle);

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux