How to view an image outside of web root? - php

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 modern browser.</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));?>">

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.

Related

Issue in loading image file in PHP

I am facing some typical issue in loading image files in PHP. Image got successfully loaded but cannot get displayed properly.
I have declared a define value, then echoed it in an img tag src attribute :
define('image_site_url','localhost/projects/faantush/product_images/');
<img src="<?php echo image_site_url.$row['image1'] ?>" />
The image file is not found but it is there in product_images. Here are the pictures:
In the HTML code: You are missing the: http:// before localhost
<img src="http://localhost/ ...
If your pictures are hosted on the same server than your website, you don't really need to use an absolute path. You could use a relative path and remove the domain name (localhost in this case):
<img src="/projects/path/to/your/picture.jpg" />
You must use absolute paths when linking to another website, but you can also use absolute paths within your own website. This practice is generally frowned upon, though. Relative links make it easy to do things like change your domain name without having to go through all your HTML pages, hunting down links and changing the names. As an added bonus, they force you to keep your site structure neat and organized, which is always a good idea.
An interesting use case of not using absolute paths is, if you want to set your website to SSL/TLS: you will have to change all the occurrences of http to https. Although it is not a big deal, this is generally not the kind of work you want to do.
See Relative path or url for html src and href attributes and the post from where this quote is taken.

Lumen's url() function performing strangely

I recently did a fresh install of Lumen framework and started building a site from it. Yes I know lumen is designed only for APIs but please help me out with this.
I have placed all my views inside /resources/views and my templates inside /resources/views/templates.
Now since I had to place [css/js/images] somewhere, I thought placing all of that in public/assets/[css/js/images] would be nice.
Now in my layout when I am trying to include css - I use something like this -
<link href="{{ url('assets/css/something.css') }}">
and it works giving an output of something like this -
<link href="localhost/assets/css/something.css">
same thing works for js also but it gets strange when I try to include images. For including an image I write something like -
<img src ="{{ url('assets/images/someimage.jpg') }}"
and when I view page source, output is as I expect it to be -
<img src="localhost/assets/images/someimage.jpg">
but my console fires 404 not found errors stating someimage.jpg not found. And when I crosscheck by inspecting the image's parent, the Url is totally different soemthing like this -
<img src="localhost/images/someimage.jpg">
See, automatically omitting 'assets' from image url and resulting in 404, but I can see correct url when I view page source.
Things I tried to resolve the issue -
Cleared cache and reloaded the page.
Tried using asset() instead of url() but prototype of that was removed from lumen.
Pull out [css/js/images] folder from assets and pasted them in parent i.e. public. This worked but then the question is why did the previous setup worked find for both css and js and caused problem only with images ?
My other questions are -
1. How can the url in page source be different from the one being rendered ? Just to mention in my case the url in page source worked and displayed image but since the url being renderred omitted 'assets' from path hence resulted in 404.
2. Is there any other good way to include these assets in views. If yes then please also mention where to put them ?
Attaching some images/code for reference.
Output of rendered page showing 404 errors for images but none for css.
Output of view page source windows showing asset included in image path
No clue if this is right, but I believe you actually need to put an image stream inside that URL. Now the server is trying to retrieve some byte encoded object that isn't there.
I have no idea if it's the same case for you, but I've had many instances where I had to put in image streams instead of URLs, which I've solved this way using this library:
Routes.php
/**
* Image handling routes.
* These make sure images actually are images instead of bytecode
*/
Route::get('organization/logo/{logo}', function($logo) {
$file = Image::make(Storage::disk('logo-image')->get($logo));
return $file->response();
});
View
<img src="{{ asset('organization/logo/' . $organization->logo_path) }}" alt="">
I might be completely off, but I recognize what's happening with your app and this took care of the issues when I implemented it.
Check your web server configuration. It sounds like you have some type of redirect setup that redirects assets/images/* to just images/*.
As a simple test, open your "Network" tab and navigate your browser to http://samplelumena.local/assets/images/footer1.jpg. I'm guessing the Network trace will show a 30x (301, 302, etc.) to http://samplelumena.local/images/footer1.jpg, followed by the 404 for that url.

Load content from website to MATLAB including internal frames (iframe)

I'm using urlread in MATLAB to load the content of websites and then search that for further use.
However, I came across a site on which the content I need is located in a specific internal frame which is embedded in the index.php through <iframe>.
When I use urlread with the url of the file which is seemingly embedded in the main file, I still only get the content of that index.php instead of that of the targetframecontent.php.
I guess urlread might not be the correct tool for this or I might be overlooking other things which make my attempts unsuccessful.
Is there a way to get the content of such an internal frame into MATLAB?
EDIT: A bit more precisely:
I would usually use the following to read in the content of a website to my MATLAB workspace:
data = urlread('http://[...]index.php')
But doing that I only get the content of the parent index.php and not that of the embedded targetframecontent.php. I figured that out when going through the websites source code, where the part of the website which includes the content I'm interested in gets loaded into a frame with:
<iframe src="http://[...]targetframecontent.php" width="850px" height="1000px" border="0" frameborder="0"></iframe>
Because of that I tried to use urlread directly with the url which is loaded in the specific target frame.
However, when I did this I still got the same content as if I was still loading the index.php.
So it seems to redirect urlread back to index.php whenever I try to use the direct url of targetframecontent.php. That is also what happened when I tried to load targetframecontent.php in a browser.
Thanks for any help.
It looks like the site in question checks the Referrer of the HTTP request. Using wget, if you pass the --referer option, you can download the <iframe>:
$ wget --referer="http://www.sf.tv/sfmeteo/lokalprognosen/index.php?q=Gen%C3%A8ve" -O test_ref "http://www.sf.tv/sfmeteo/lokalprognosen/detailprognose.php?id=&q=Gen%C3%A8ve&max=&drs=0&kiosk=&js=&deeplink=&f="
$ cat test_ref
[...]
<body class="sf">
<!-- referrer:http://www.sf.tv/sfmeteo/lokal -->
<script type="text/javascript"> <!--
var keineprognose = '0';
// -->
</script>
<p class="inv">
zum Inhalt<br />
</p>
However, if you wget without sending a referrer, you get:
wget -O test_noref "http://www.sf.tv/sfmeteo/lokalprognosen/detailprognose.php?id=&q=Gen%C3%A8ve&max=&drs=0&kiosk=&js=&deeplink=&f="
<body class="sf" role="application">
<p class="inv">
zum Inhalt<br />
</p>
<!--googleoff: all--> <div id="HEADWRAP" class="sf-header">
<div class="INNERWRAP">
<h1 class="inv">SF Schweizer Fernsehen - Navigation</h1>
Which is the original, parent page.
Therefore, to suck this into MATLAB, you'll need to set the Referer HTTP header. Unfortunately I don't have MATLAB in front of me right now, but this page looks like it gives a good introduction to how to send headers with MATLAB HTTP requests: http://undocumentedmatlab.com/blog/expanding-urlreads-capabilities/
Edit: Because it's not super-obvious from that page, here's a link to the urlread2 function, which also includes some examples: http://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2/content/urlread2.m

Load page within the page

I'm a beginner in PHP and Javascript..
I found a link from http://cmichaelis.whsites.net/whblog/jquery-extjs-1/example2
Inside it there is a code saying :
function addPanel(location)
{
tabpanel.add({
autoLoad: {url: location},
title: 'More Information...',
closable:true,
autoScroll:true
}).show();
}
how to use :
<a href="javascript:void(0);"
onclick="addPanel('loadpage.php?a=http://www.google.com')">
head over to Google
</a>
What I want to ask is.. what is the code for loadpage.php?
The PHP page does not echo out the contents of google.com as suggested in the other answer. It outputs an iframe that points to Google:
<iframe src="http://www.google.com" width="100%" height="100%" frameborder="no"></iframe>
It looks like loadpage.php could be in use to echo out the contents of www.google.com, using file_get_contents.
loadpage.php:
<?php
// Simplified output - should sanitise $_REQUEST params etc first..
echo file_get_contents($_REQUEST['a']);
?>
loadpage is effectively acting as a proxy, allowing your javascript to call pages which are not on your own domain.
As #annakata points out in the comments, the code above is obscenely dangerous as-is. The code is an illustration of the basic idea behind a proxy file - in production, this file would need to make sure that the $_REQUEST parameters were sanitised, e.g. only accept values from a whitelist.
The same origin policy is a security element of javascript that stops you from pulling content from outside your domain on to your page using javascript.
Some sites get around this by calling a proxy page on their own server (loadpage in this instance) which effectively just prints out the content of a target url. As this proxy page is on your server, this by-passes the same origin security issue, and still makes available the content of a page from another domain - here www.google.com
Oops, I somewhat foolishly didn't RTFA, but just the code in the question and hypothesised at what it could be doing. #andynormancx is right in his answer as to what the page linked in the q is actually doing.

How to keep a website with url routing directory independent

I'm developing a PHP website that uses url routing. I'd like the site to be directory independent, so that it could be moved from http://site.example.com/ to http://example.com/site/ without having to change every path in the HTML. The problem comes up when I'm linking to files which are not subject to routing, like css files, images and so on.
For example, let's assume that the view for the action index of the controller welcome contains the image img/banner.jpg. If the page is requested with the url http://site.example.com/welcome, the browser will request the image as http://site.example.com/img/banner.jpg, which is perfectly fine. But if the page is requested with the url http://site.example.com/welcome/index, the browser will think that welcome is a directory and will try to fetch the image as http://site.example.com/welcome/img/banner.jpg, which is obviously wrong.
I've already considered some options, but they all seem imperfect to me:
Use url rewriting to redirect requests from (*.css|*.js|...) or (css/*|js/*|...) to the right path.
Problems: Every extension would have to be named in the rewrite rules. If someone would add a new filetype (e.g. an mp3 file), it wouldn't be rewritten.
Prepend the base path to each relative path with a php function. For example:
<img src="<?php echo url::base(); ?>img/banner.jpg" />
Problems: Looks messy; css- and js-files containing paths would have to be processed by PHP.
So, how do you keep a website directory independent? Is there a better/cleaner way than the ones I came up with?
You could put in the head
<base href="<?php echo url::base(); ?>" />
This will mean the browser will request any non-absolute URLs relative to that path. However I am not sure how this would affect URLs embedded in CSS files etc. This does not affect paths defined in CSS files. (thanks mooware)
The <base> thing will work but you need to remember it's going to affect your <a> tags too. Consider this example.:
<!-- this page is http://oursite.com/index.html -->
<html>
<head>
<base href="http://static.oursite.com/" />
</head>
<body>
<img src="logo.gif" alt="this is http://static.oursite.com/logo.gif" />
this links to http://static.oursite.com/login which is not what we wanted. we wanted http://oursite.com/login
</body>
</html>
If you use a PHP function call for creating your links, that won't be a problem as you can just make sure it spits out absolute URL. But if you (or your designers) hand-code the <a> tags then you're stuck with the same problem again, just now with <a> instead of <img>.
EDIT: I should add the above paragraph is assuming you serve images from a different host name like we do. If you don't then obviously that won't be a problem.
tomhaigh has a good point, and would be worthwhile to investigate it further.
According to MSDN, the base tag works for all external sources, including style sheets, images, etc.
Perhaps I'm missing something, but can't you just do what I (and I thought everybody else) do/es? Namely put all your images, css, javascripts, etc in a common directory i.e.:
/inc/images/
/inc/css/
/inc/javascript/
etc
And then reference them with base-relative URLs, i.e.:
<img src="/inc/images/foo.jpg" />
etc
?

Categories