Render HTML pages from private folder in PHP - php

I have a web site that have HTML pages stored in a private folder. I want a PHP script that can read the HTML file then push it to the browser.
My tought was to get the html file with the file() function in PHP. Then echo() it to the browser. That works for the html content of the page. The images and the css does not follow however.
I heard of a "render" function in IIS or ASP that render the HTML content of a web page in a private folder then send the images in a binary format. Does PHP have something similar?
Currently I read the file as follow :
$htmlFile = file(PATHTOFILE);
echo(implode('',$htmlFile));
The reason we are trying to do that is to protect the url / information of the pages contained in this folder. The user will have to connect to the web service, then the PHP script will push the html pages

You can use the tag base to solve the problem of the relative path of the files, something like this:
$html = file_get_contents($url);
$html = str_replace('<head>', '<head><base href="FULL PATH OF DIR" />', $html);
echo $html;

CSS and images are not displayed because their paths in the HTML files is relative to HTML files, right? And if you have these CSS and images in the same private folder, how can you hope the user will fetch them?
Indirect, you should fetch CSS and images the same way you do with HTML. But this means you have to replace all paths in your displayed HTML, that is quite absurd. In fact, we are talking about some kind of proxy now... ?!?!?
Why you need it?
Anyway echo(file_get_contents($htmlFile)); is less stressful.

Another option if it is an <img /> tag and the image is also stored outside of the root you can just make the src= attribute as so:
src="get_image.php?file=thisfile.png" // add a $_GET if needed to distinguish files
then get_image.php:
$file = $_GET['file'];
// security checks if you wish
header(sprintf("Content-type: %s;",'image/png'));
readfile($file);
exit;

Related

How to load images into a page using a php file but wait for all images load before displaying

This is the minimum code in order to demonstrate the effect.
https://replit.com/#computinginschools/imageload#index.php
This is a simple php file which loads image using an external script, image.php, and displays them on the page. The reason I have done this on my live site is because the images are stored outside the www folder and I need the image.php script to access them. To make it easier I have just put everything under the webroot.
In this example, all the images are located in a folder called images next to index.php.
<html>
<head>
<title>Image Load</title>
</head>
<body>
<?php
$images = glob('images/*.png');
foreach($images as $image){
echo('<img src="image.php?i='.urlencode($image).'" loading="lazy">');
}
?>
</body>
</html>
This is the image.php file which loads the images.
<?php
$path = urldecode($_GET['i']);
if (file_exists($path)) {
$extension = pathinfo($path)['extension'];
header('Content-type:'.$extension);
header('Cache-control:public, max-age=604800');
header('Accept-ranges:bytes');
header('Content-length:'.filesize($path));
header('Last-Modified: '.date(DATE_RFC2822, filemtime($path)));
header_remove('pragma');
echo(file_get_contents($path));
}
else {
echo("File not found: ".$file);
}
?>
The script works fine to display the images however, the loading is often prolonged with all the images loading in a 'wave' as the image.php file renders them on the screen after index.php has loaded. The REPL is fast so this is not really noticeable, however, on my production site, this effect is quite pronounced.
I would like to know if there is a way of delaying the loading of the images in index.php until all the images rendered by images.php have loaded. My intention is to set the display attribute of the images to none by default and then, when the index.php document has loaded, apply an appear animation to all the images on the page to make them fade into view.
It would be even better if I could embed some kind of loading animation in the container for each image.
Please help?
This can't really be done in PHP; the slowdown is from the browser going through the HTML and loading all of the images. If you would like to hide the page until everything is loaded, then you can set the HTML body to display: none, and the use JavaScript to remove that once all of the images are loaded. This has to be done from JavaScript because there isn't really any way to check on the browser's status from PHP.
If you must do this from PHP, then you can have all of the images inline using base64 encoding, although this is just about the least efficient way of serving images, and will probably cause major slowdowns. The format to do so is like this:
<img src="data:image/jpeg;base64,<?php echo base64_encode(file_get_contents($image)); ?>">

PHP include svg assets in cake php

Does cake have a way to php include svg assets? I know how to use helpers to create an <img> tag pointing to the SVG for the img's src attribute, but I'd like to actually include the file rather than reference it within an <img> tag.
No, CakePHP doesn't ship with such functionality, you'll have to come up with something on your own, or use one of the many PHP based SVG inliners out there, it should be easy enough to wrap that in a custom helper.
If you just need to embed the file, then you could even stick to simply reading and outputting the file contents with the XML declaration and doctype removed, something like:
$svg = file_get_contents($path);
$svg = preg_replace('/^<\?xml.*?\?>\s*(<!DOCTYPE.*?>\s*)?/is', '', $svg);
In the end, this was a silly question. You can of course just use <?php include 'img/thefile.svg' ?> assuming your svg is in webroot/img folder. If the svg or file is not in a publicly accessible folder I would look to creating a custom helper as another post suggested.

How to display photo from non-public folder?

I have built my website in a way so that the actual database files are positioned as follows
root/
root/httpdocs <- public folder
root/databasefolder/actual_files_here/some_photo.jpg
How can I implement so that the php can view/load a photo from that databasefolder and echo it with tag. I simply tried "../databasefolder/" in a tag, but it doesn't work (of course it doesn't), since it's from hidden folder.
I tried Base64 which works in other browsers, but not with Internet Explorer, due to that silly kb limit.
In your HTML:
<img src="image.php?image=some_photo.jpg" />
And a php script that echoes your image.
header('Content-type: image/jpeg');
echo imagejpeg('root/databasefolder/actual_files_here/' . $_GET['image']);
You can use the get parameter "image" to load whatever image you need from that folder.

How do I download this type of file using php?

So,I'm trying to automate downloading images from picjumbo.com site.So far most things worked like finding img src and find it's image etc.But when I try to download the image all I get is an html file(open those files with notepad++ to view it).How do I download the file after loading that page? I'm putting my code below.Everything works except getting that final image! :(
How do I download that image ?
file Download page : http://picjumbo.com/download/?d=IMG_3642.jpg
How do I save that image after the page load??
thanks!
<?php
include("simple_html_dom.php");
$file = "http://picjumbo.com/";
$files = file_get_contents($file);
$html = new simple_html_dom();
$html->load($files);
foreach($html->find('img[class=image]') as $element){
$img_src = explode('/',$element->src);
$img_src = explode('-',$img_src[5]);
$img = $img_src[0];
$url = 'http://picjumbo.com/download?d='.$img.'.jpg';
copy($url, 'images/'.$img);
}
The URL redirects to a page that uses Javascript to start a separate download. If you watch what happens in the Network tab of the browser's Developer Tools, you'll see that the actual URL of the image is:
http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d=$img.jpg
The reason you're getting an HTML page when you try to download that link instead of the image is because that page is an HTML page, not an image. If you click on it, it takes you to an HTML page rather than to an image. Now once that page is loaded, it redirects you to downloading the image using this on line 12 of the HTML code:
<meta http-equiv="refresh" content="0; url=http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d=IMG_3642.jpg">
So the URL image is this:
http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d=IMG_3642.jpg
In order to download this image, you'd have to gather this HTML page into a variable, and parse through it in some way to grab the URL from this, and then use file_get_contents() to download the image. To parse through this, you could use something like SimpleHTMLDOM to read it in, or since you're just looking for the one tag, if it appears consistently in the document, you could just pull out line 12 then use some creative substr() action to get the URL.

PHP: Get all CSS files of an HTML web page

I'm trying to get all CSS files of an html file from URL.
I know that if I want to get the HTML code it is easy - just using PHP function - file_get_contents.
The question is - if I could search easily inside an a URL of HTML and get from there the files or content of all related CSS files?
Note - I want to build an engine for getting a lot of CSS files, this is why just reading the source is not enough..
Thanks,
You could try using http://simplehtmldom.sourceforge.net/ for HTML parsing.
require_once 'SimpleHtmlDom/simple_html_dom.php';
$url = 'www.website-to-scan.com';
$website = file_get_html($url);
// You might need to tweak the selector based on the website you are scanning
// Example: some websites don't set the rel attribute
// others might use less instead of css
//
// Some other options:
// link[href] - Any link with a href attribute (might get favicons and other resources but should catch all the css files)
// link[href="*.css*"] - Might miss files that aren't .css extension but return valid css (e.g.: .less, .php, etc)
// link[type="text/css"] - Might miss stylesheets without this attribute set
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet)
{
$stylesheet_url = $stylesheet->href;
// Do something with the URL
}
You need to parse the HTML tags looking for CSS files. You can do it for example with preg_match - looking for matching regex.
Regex which would find such files might be like this:
\<link .+href="\..+css.+"\>

Categories