How to view an image outside of web root?

2019-07-27 09:47发布

问题:

I have a .png that I want to embed on a particular page. This .png is outside of the web root, but we have a module in our system that allows users to view that image on a separate page. I found that I can use an iframe to view the .png on some browsers, but sometimes there is an authorization issue or when trying to print the page the image doesn't load through the iframe. I tried PHPs readfile(), but then I just have another authorization issue. I find the authorization issue odd since one has to be logged in to use our site at all. What would be the best way to imbed this image?

<?php foreach($activity->getMedias() as $media_count => $media): 



        <iframe id="content" src="http://www.mysite.com/media/view/id/<?php echo $media->getId(); ?>" frameborder="0" style="width: 100%; height:825px;" seamless="seamless">
            <p>Your browser does not support iframes. Please upgrade to <a href="http://getfirefox.com">a modern browser</a>.</p>
        </iframe>

SOLVED This is the code I used to get it to work. I am using Symfony 1.4.

<?php foreach($activity->getMedias() as $media_count => $media): 

        $dataString=sfConfig::get('sf_root_dir').'/media/'.$media->getLocation(); ?>


        <img SRC="data:image/png;base64,<?php echo base64_encode(file_get_contents("../../../../../..".$dataString));?>">

回答1:

Sounds like you're trying to pass a url to readfile, e.g.

readfile('http://example.com/foo/bar');

This causes PHP to start up a full-blown HTTP request back to itself. You need to use a local file-system path, e.g.

readfile('/path/outside/doc/root/to/the/image.png');

That's a purely local filesystem operation, and does not involve the HTTP layer at all, bypassing the entire document root/http authentication business.