Set backgroung image in .php file - php

I downloaded this code:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
// ADD SPECIFIC FILES YOU WANT TO IGNORE HERE
$ignore_file_list = array( ".htaccess", "Thumbs.db", ".DS_Store", "index.php", "flat.png", "error_log" );
// ADD SPECIFIC FILE EXTENSIONS YOU WANT TO IGNORE HERE, EXAMPLE: array('psd','jpg','jpeg')
$ignore_ext_list = array( );
// SORT BY
$sort_by = "name_asc"; // options: name_asc, name_desc, date_asc, date_desc
// ICON URL
//$icon_url = "https://www.dropbox.com/s/lzxi5abx2gaj84q/flat.png?dl=0"; // DIRECT LINK
$icon_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAAyCAYAAADP7vEwAAAgAElEQVR4nOy9d5hdV3nv";
// TOGGLE SUB FOLDERS, SET TO false IF YOU WANT OFF
$toggle_sub_folders = true;
// FORCE DOWNLOAD ATTRIBUTE
$force_download = true;
// IGNORE EMPTY FOLDERS
$ignore_empty_folders = false;
// SET TITLE BASED ON FOLDER NAME, IF NOT SET ABOVE
if( !$title ) { $title = clean_title(basename(dirname(__FILE__))); }
?>
Th full code can be download here: https://github.com/halgatewood/file-directory-list/blob/master/index.php
I'm having problem with the start:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
I want to put a picture as background, but it isn't happening. What's wrong?
Changed with the answer:
<?php
echo "<div style='background-image: url('/bg.jpeg');' ></div>";
?>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
etc..

No need for all that,
What you desire to achieve is much simpler.
Assuming this code is inside index.php and your server's directory structure:
/some-folder/
/index.php
/bg.jpeg
Simply link it as its done in plain html —
<?php
echo "<div style=\"background-image: url('/bg.jpeg');\" ></div>";
?>
If you wan't it do be dynamic, i.e, image files's name is inside a variable then,
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
Update:
Important Tip: All programming languages are executed line-by-line, this tip applies not only to PHP, but also HTML Learn More
Assume for example, your page's html structure returned to the browser is as provide below and you want to apply background to body tag
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Simply copying and pasting my code to the top of page will result in
<div style="background-image: url('bg.jpeg');" ></div>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
But that created a empty div tag at the top of html output, i wanted it to apply background to by body tag instead !!!?
— This happened because echo is used to send output to the browser as soon as it is executed. So since you copied my code to the top of your script the html output is also at the top.
But why did it echo <div style="background-image: url('bg.jpeg');" ></div> when i wanted it to apply to my page's body?
— Because the echo statements reads "<div style=\"background-image: url('bg.jpeg');\" ></div>"; as its output.
Ok, but how to apply the background-image to body then??
As mentioned earlier code is executed line-by-line, so in order to apply the style to pages's body tag you'll need to call it near your body tag and also modify it to not output the div it currently does.
So assuming your index.php is:
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
You'll need to change it to —
<?php
$my_image = 'bg.jpeg';
// don't echo any thing here
?>
<html>
<head><head>
<body style="background-image: url('<?php echo $my_image; ?>')">
<!-- apply the style to body -->
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Hopefully i explained it well :)

Related

How to check if text is present on a webpage?

how to check if text is present on a webpage using php and if true to execute some code?
My idea is to show some relevant products on the confirmation page after completing an order - if the name of the product is present on the page, then load some products. But I can't make the check for present text.
Case 1 if you prepare your page in a variable then echo it at the end of the script like
$response = "<html><body>";
$response .= "<div>contents text_to_find</div>";
$response .= "</body></html>";
echo $response;
then you can merely search the string with any string search function
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
Case 2 if you don't prepare the page in a string . and you just echo the contents and output the contents outside the <?php ?> tags like
<?php
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
Then you have no way to catch the text you want unless you use output buffering
Case 3 if you use output buffering - which I suggest - like
<?php
ob_start();
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
then you can search the output anytime you want
$response = ob_get_contents()
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
You may need to buffer your Output like so...
<?php
ob_start();
// ALL YOUR CODE HERE...
$output = ob_get_clean();
// CHECK FOR THE TEXT WITHIN THE $output.
if(stristr($output, $text)){
// LOGIC TO SHOW PRODUCTS WITH $text IN IT...
}
// FINAL RENDER:
echo $output;
Fastest solution is using php DOM parser:
$html = file_get_contents('http://domain.com/etc-etc-etc');
$dom = new DOMDocument;
$dom->loadHTML($html);
$divs = $dom->getElementsByTagName('div');
$txt = '';
foreach ($divs as $div) {
$txt .= $div->textContent;
}
This way, variable $txt would hold the text content of a given webpage, as long as it is enclosed around div tags, as usually. Good luck!

PHP script tags

Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.

PHP link to include header

I have php reading a text file that contains all the names of images in a directory, it then strips the file extension and displays the file name without the .jpg extension as a link to let the user click on then name, what I am looking for is a easy way to have the link that is clicked be transferred to a variable or find a easier solution so the link once it is clicks opens a page that contains the default header and the image they selected without making hundreds of HTML files for each image in the directory.
my code is below I am a newbie at PHP so forgive my lack of knowledge.
thank you in advance. also I would like a apple device to read this so I want to say away from java script.
<html>
<head>
<title>Pictures</title>
</head>
<body>
<p>
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = ' PHP';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('header.php');
?>
<center>
<?php
// loads page links
$x="0";
// readfile
// set file to read
$file = '\filelist.txt' or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
$page[$x]=$line;
$x++;
}
$x--;
for ($i = 0; $i <= $x; $i++)
{
$str=strlen($page[$i]);
$str=bcsub($str,6);
$strr=substr($page[$i],0,$str);
$link[$i]= "<a href=".$page[$i]."jpg>".$strr."</a>";
echo "<td>".$link[$i]."<br/";
}
?>
</P></center>
<?php
// include the page footer
include('/footer.php');
?>
</body>
</html>
add the filename to the url that you want to use as a landing page, and catch it using $_GET to build the link.
<a href='landingpage.php?file=<?php echo $filename; ?>'><?php echo $filename; ?></a>
Then for the image link on the landing page
<img src='path/to/file/<?php echo $_GET['file'] ?>.jpg' />

Is it possible to add a <script> tag into HTML with PHP str_replace?

I have en exercise Im working on right now where we can't use libraries. I have a REST based system that instantiates different PHP classes depending on what needs to be done. I have several HTML files that are loaded into PHP and then using str_replace I switch out the variables I want to inject into the HTML.
I now want to add a tag at the end of one of my HTML files. My ../../html/body.html file looks like this:
<div id="content-wrapper">
<div id="content">
<table>
<!-- $content -->
</table>
</div>
</div>
</div>
<!-- javascript -->
</body>
</html>
I use this "body" page several times so the " $content " varies depending on what I replace them with in PHP.
Now for some reason when I try to replace javascript with a script tag it doesn't work. Heres my PHP that is trying to do this:
$javascript = '<script type="text/javascript" language="javascript" src="../../js/subscriber.js"></script>';
$page = file_get_contents("../../html/body.html");
$head = file_get_contents("../../html/doctype.html");
$nav = file_get_contents("../../html/Navbar.html");
$page = str_replace('<!--javascript -->', $javascript, $page);
$page = str_replace('<!-- $content -->', $theFeed, $page);
$nav = str_replace('<!-- $name -->', $this->user, $nav);
$nav = str_replace('$user', $this->you, $nav);
$nav = str_replace('$key', $this->key, $nav);
$nav = str_replace('$picUrl', $picUrl, $nav);
echo($head);
echo($nav);
echo($page);
I was thinking that this way I only need to include scripts on the necesarry pages or not at all if the page doesn't need any javascript. Does PHP or HTML block script tags somehow? Im not sure how to get around this. Thanks!
You're missing a space. Your markup contains:
<!-- javascript -->
^ space here
But your PHP code is missing that space:
$page = str_replace('<!--javascript -->', $javascript, $page);
Change it to:
$page = str_replace('<!-- javascript -->', $javascript, $page);
I think your problem is that in the PHP you have no space after

PHP $_server name and uri

Hi I'm trying to get a piece of html to only show on the main page which is http://www.domain.com/ ... I wrote the code below but it doesn't work the HTML is showing regardless of the page, am I missing something
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
?>
<div style="margin:0 auto;">
<div style="float:left">
<?php endif; ?>
First of all - please change
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
into
$hweb = 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$hweb may be initialized somewhere before.
Second:
As long as you request 'http://www.domain.com/somename.php' your if condition will never get executed. REQUEST_URI will always hold '/somename.php' except you use some url rewriting.
Third:
Make sure all calls go to 'http://www.domain.com' and not to 'http://domain.com'. Subdomain configurtaions sometimes are very complicated.
At the risk of getting it wrong again..
Why not initialize a variable in the main file before including the header
<?php
$mainfile = true;
?>
then in the header
<?php
if ($mainfile===true)
....
This way the main file can be called anything and be placed anywhere.
Solution 1:
If the above code is written inside 'http://www.domain.com/index.php' file then it may work fine.
Solution 2:
else make sure that $hewb is set with null value earlier b4 this code so that ".=" would not add extra value b4 'http...'.
Now
$hweb = '';
echo $hweb .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
That is because the the HTML is inline in the php file but outside of the PHP tags. You can simply echo the HTML inside the if.
if ($hweb == 'http://www.domain.com/')
{
echo '<div style="margin:0 auto;">';
echo '<div style="float:left">';
}
or if you have lots of HTML you could do it like this
<?php
ob_start();
?>
<html>
<body>
<p>This HTML only be echoed </p>
</body>
</html>
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
{
ob_end_flush();
}
else
{
ob_end_clean(); // Probably not needed
}
?>

Categories