I have created separate HTML and jquery sites for iPhone and Ipad. Cause I have used Heroku for deployment I have to have a PHP index. So I have included Html inside PHP. It loads the index.html inside there's folders.but the links are broken( CSS, Images ).
Is there any solution of alternative for that?
<?php
echo $width = "<script>document.write(screen.width);</script>";
echo $height = "<script>document.write(screen.height);</script>";
if($width!=null && height !=null) {
if($width>376 && $height>812){
include_once("ipad/index.html");
} else {
include_once("iphone/index.html");
}
} else {
echo json_encode(array('outcome'=>'error','error'=>"Couldn't redirect. Redirecting to iPhone version"));
}
?>
Note -
I have tried something like this
<?php
echo $width = "<script>document.write(screen.width);</script>";
// echo $height = "<script>document.write(screen.height);</script>";
if($width!=null) {
$path = $_SERVER['DOCUMENT_ROOT'];
if($width>376){
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/StanstedGo/ipad/index.html";
include_once($path);
} else {
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/StanstedGo/iphone/index.html";
include_once($path);
}
} else {
echo json_encode(array('outcome'=>'error','error'=>"Couldn't redirect. Redirecting to iPhone version"));
}
?>
but it also does not solve my problem. I still have broken links to CSS files.
Related
Im setting up a xampp php website with auto creating css for site (If site named xyz.php/html is created, then a css is created too). Unfortunatelly css doesn't want to include in website using php echo's and html tags. No error.
In style.php:
$arr = explode("/",$_SERVER['PHP_SELF']);
$style = "";
foreach ($arr as $key){
if(strpos($key, ".php")){
$style = str_replace(".php", "style.css", $key);
}
}
if($fp = fopen($_SERVER['DOCUMENT_ROOT']."/TestPHP/".$addr,"wb+")){
fwrite($fp,"body{background-color:#666;}");
fclose($fp);
}
echo $addr = "lib/require/styles/".$style;
echo '<link href="'.$addr.'" rel="stylesheet">';
In index.php:
require_once 'lib/require/php/styles.php';
That's because the only HTML (as i can see in your code) doesn't have anything else than the style, no head, no body... Why don't you directly paste the HTML inside the file instead of making PHP echo it?
I am storing my files on Amazon Cloud Drive. And there are option for setting response-content-disposition to direct links from Amazon.
From the API:
response-content-disposition: (Optional) If specified, the content-disposition of the response will be set to this value
I need to set it download mp3 files. I don't need to play it on browser.
And I want to set downloaded file name: Эльбрус Джанмирзоев - Кавказская любовь.mp3
I tried many ways... Urlencode... Urlencode + http build query etc...
But now the result is: Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3 when downloading files.
And here is my php code:
<?php
function file_generate_download_link($filename = false, $force = true)
{
$json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
$url = $json['tempLink'];
if($force)
{
$data['download'] = 'true';
}
if($filename)
{
$data['response-content-disposition'] = ' attachment; filename*=UTF-8\'\''.urlencode($filename).'';
}
if(isset($data)) {
$data = http_build_query($data);
$url .= '?' . $data;
}
return $url;
}
echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>
This code returns me this link:
https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb?download=true&response-content-disposition=+attachment%3B+filename%2A%3DUTF-8%27%27%25D0%25AD%25D0%25BB%25D1%258C%25D0%25B1%25D1%2580%25D1%2583%25D1%2581%2B%25D0%2594%25D0%25B6%25D0%25B0%25D0%25BD%25D0%25BC%25D0%25B8%25D1%2580%25D0%25B7%25D0%25BE%25D0%25B5%25D0%25B2%2B-%2B%25D0%259A%25D0%25B0%25D0%25B2%25D0%25BA%25D0%25B0%25D0%25B7%25D1%2581%25D0%25BA%25D0%25B0%25D1%258F%2B%25D0%25BB%25D1%258E%25D0%25B1%25D0%25BE%25D0%25B2%25D1%258C.mp3
If I enter this link Chrome saves this file with this name:
Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3
But I need excatly save file with this name:
Эльбрус Джанмирзоев - Кавказская любовь.mp3
Where is my mistake?
Gotcha! I need to use rawurlencode function!
So the right code is:
<?php
function file_generate_download_link($filename = false, $force = true)
{
$json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
$url = $json['tempLink'];
if($force)
{
$data['download'] = 'true';
}
if($filename)
{
$data['response-content-disposition'] = ' attachment; filename*=UTF-8\'\''.rawurlencode($filename).'';
}
if(isset($data)) {
$data = http_build_query($data);
$url .= '?' . $data;
}
return $url;
}
echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>
I would just like to hear if it is possible to echo something on one page, and not every other page, even though they all get content from another file.
I have 3 pages, and they all have this in code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
include_once($path);
?>
If I want "index.php" to echo something that "about.php" does not echo, can you then do it?
As the title says, I'd guess on something like this:
if filename == index.php then
echo=hello
I wouldn't make it so specific If you add more and more pages what will you do? Handle them as special cases one by one? Now you have to handle two pages, after three months they'll be 10...and so on.
It's not a perfect solution but as starting point you may use a flag. Beginning like this:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/index.php";
$helloWorld = TRUE;
include_once($path);
?>
And in your index.php include file:
if (helloWorld === TRUE) // or if (!!helloWorld)
echo "Hello world";
if (strpos($_SERVER['SCRIPT_NAME'],'index.php') !== false) {
echo "do stuff";
}
The script I am using 'gets' a html page and parses is showing only the .jpg images within, but I need to make some modifications and when i do it simply fails...
This works:
include('simple_html_dom.php');
function getUrlAddress() {
$url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $url .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$html = file_get_html($url);
foreach($html->find('img[src$=jpg]') as $e)
echo '<img src='.$e->src .'><br>';
However, there are some problems... I only want to show images over a certain size, plus some site do not display full URL in the img tag and so need to try to get around that too... so I have done the following:
include('simple_html_dom.php');
function getUrlAddress() {
$url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $url .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$html = file_get_html($url);
foreach($html->find('img[src$=jpg]') as $e)
$image = $e->src;
// check to see if src has domain
if (preg_match("/http/", $e->src)) {
$image = $image;
} else {
$parts = explode("/",$url);
$image = $parts['0']."//".$parts[1].$parts[2].$e->src;
}
$size = getimagesize($image);
echo "<br /><br />size is {$size[0]}";
echo '<img src='.$image.'><br>';
This works, but only returns the first image.
On the example link below there are 5 images, which the first code shows but does not display them as the src is without the leading domain
Example link as mentioned above
Is there a better way to do this? And why does the loop fail?
You seem to be missing a {:
foreach($html->find('img[src$=jpg]') as $e) {
You forgot your brackets:
foreach($html->find('img[src$=jpg]') as $e){
$image = $e->src;
// check to see if src has domain
if (preg_match("/http/", $e->src)) { $image = $image; }
else {
$parts = explode("/",$url);
$image = $parts['0']."//".$parts[1].$parts[2].$e->src;
}
$size = getimagesize($image);
echo "<br /><br />size is {$size[0]}";
echo '<img src='.$image.'><br>';
}
I am trying to scrape img src's with php, I can get the src fine, but if the src does not include the full path then I can't really reuse it. Is there a way to grab the full path of the image using php (browsers can get it if you use the right click menu).
ie. How do I get a FULL path including the domain in one of the following two examples?
src="../foo/logo.png"
src="/images/logo.png"
Thanks,
Allan
You don't need a regex... just some patience. I don't really want to write the code for you, but just check if the src starts with http://, and if not, you have like 3 different cases.
If it begins with a / then prepend http://domain.com
If it begins with .. you'll have to split the full URL and hack off pieces until the src starts with a /
Else (it begins with a letter), the take the full domain, and strip it down to the last slash then append the src URL.
Or.... be lazy and steal this script
$url = "http://www.goat.com/money/dave.html";
$rel = "../images/cheese.jpg";
$com = InternetCombineURL($url,$rel);
// Returns http://www.goat.com/images/cheese.jpg
function InternetCombineUrl($absolute, $relative) {
$p = parse_url($relative);
if($p["scheme"])return $relative;
extract(parse_url($absolute));
$path = dirname($path);
if($relative{0} == '/') {
$cparts = array_filter(explode("/", $relative));
}
else {
$aparts = array_filter(explode("/", $path));
$rparts = array_filter(explode("/", $relative));
$cparts = array_merge($aparts, $rparts);
foreach($cparts as $i => $part) {
if($part == '.') {
$cparts[$i] = null;
}
if($part == '..') {
$cparts[$i - 1] = null;
$cparts[$i] = null;
}
}
$cparts = array_filter($cparts);
}
$path = implode("/", $cparts);
$url = "";
if($scheme) {
$url = "$scheme://";
}
if($user) {
$url .= "$user";
if($pass) {
$url .= ":$pass";
}
$url .= "#";
}
if($host) {
$url .= "$host/";
}
$url .= $path;
return $url;
}
From http://www.web-max.ca/PHP/misc_24.php
Unless you have the site URL you're starting with (in which case you can prepend it to the value of the src attribute) it seems like all you're left with there is a string.
I'm assuming you don't have access to any additional information of course. If you're parsing HTML, I'd assume you must be able to access an absolute URL to at least the HTML page, but perhaps not.