Simple Snippet to read and show content from file - php

I am looking for a simple solution to add a Snippet in my index.php file to load and display the content shown in a file from an other Domain.
Plan was to add the Code to the 'Footer' before to show a floating ad on several of my websites.
Sourcesite: http://domainX.tld/floating/floater.txt
Content of file: little bit css for styling of the ad + script snippet for a close button + html to get it into shape.
Target Site gets a simple snippet to show content from txt file as its own content.
I have tried by now
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
?>
Site loads but doen't shows anything of my code.
<?php
$handle = fopen("//domainX.tld/floating/floater.txt", "rb");
$delimiter = ",";
while (!feof($handle) ) {
$line = fgets($handle);
$data = explode($delimiter, $line);
foreach($data as $v) {
echo $v;
}
}
fclose($handle);
?>
Site wouldn't even load.
<?php
$f = fopen("//domain.tld/floating/floatr.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
?>
Creates an endless amount of where my Code should be
Other Fails i have deleted already.
Once i had a simple snippet that had done the trick, does one have any idea how to accomplish that again?

This should do the trick:
<?php
echo file_get_contents('//domain.tld/floating/floatr.txt');

Sticking to the straightest way to do it as your intention is and supposing that:
URL you provide for the txt file is correct
you have read access to it
the file has contents to display
your PHP version is (PHP 4 >= 4.3.0, PHP 5, PHP 7) to support
the file_get_contents() function
You are missing in your first approach to echo the contents of your variable $StrLessDescription to send it to output.
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
echo $file_contents;
?>
Remember that for large projects you could consider using a framework to achieve the same goal in a more organized way. This is a solution to a quick-and-dirty approach you inquiry.

Related

echo content data from url source

Hey guys I'm not too knowledgeable about this particular topic in PHP yet. Basically I wanted to get a certain content of the url source so for instance the code below will only echo that specific content from the source page. I wanted to do this for other websites and the script below has errors but that's just like a demo of what I want to accomplished.
<?php
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
$data = getBetween($data,'<p class="c"> <font size="+2">',"</font></p>");
echo $data;
?>
All the information of the script above is located here
Use Simple HTML DOM to do this.
Read the manual to do this from here.
Its pretty simple.
//include simple_html_dom.php file.
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.jokesclean.com/OneLiner/Random/');
foreach($html->find('p[class=c]') as $e)
echo $e;
Just tested on my local system and it worked perfectly generating a random joke everytime i refresh
here's what i got on last refresh of this code.
.
It'd be best to use domdocument but you can also do it using regular expressions like the following.
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
if ( preg_match('/\<font size="\+2"\>(.*?)\<\/font\>/', $data, $match) ) {
echo $match['1'];
}
else {
echo 'couldn\'t find a match';
}

Contents of a file_get_content() to process PHP

I am need to use some PHP tags on a file(subfile) that is being called into a template using file_get_contents(Subfile).
I initially used require(subFile) instead of file_get_contents(). But it doesnt quite work as my content gets placed in the wrong spot. And also I can't change the template file to use require().
my template is something like this:
//TEMPLATE FILE.php
$html = file_get_contents(subFile); //I CAN NOT CHANGE THIS FILE
Then my sub file, the one I can change.
//SUB FILE.php
<div>Hello world, today is <?php echo date($mydate);?> </div>
Because it is being called with file_get_contents(), it out puts
Hello world, today is <?php echo date($mydate);?>
Instead of:
Hello world, today is Thursday.
So, considering I can't change my TEMPLATE FILE.php where the file_get_content() is used.
I was thinking if there is a way that I can wrap the content of SUB FILE.php so it would process the PHP before allowing it to be called by the TEMPLATE FILE.php.
Something like this I am after
//SUB FILE.php
<force Process the PHP ?>
<div>Hello world, today is <?php echo date($mydate);?> </div>
<Finishe Force Process ?>
<allow it to be called via file_get_content();
If I could change the template file I could use htmlspecialchars_decode(); but I can't.
Unfortunately I couldn't find a way to do that so I end up with this
ob_start();
$output = include (Dir/SubFile);
$output = ob_get_contents();
ob_end_clean();
$html = $output;
//$html = file_get_contents(subFile); The way it was
The following is an example of the official manual
$homepage = file_get_contents('http://www.example.com');
echo $homepage;
try this:
file_get_contents('http://YourDomain/Yoursubfile');
You cann't use <?php echo $a; ?> to echo the content of variable $a.
Because you will get the plain html content from your subfile.
What you can do is to:-
Step1: my_date.php
<?php
$today = date('M-d-Y');
$subfile_html = file_get_contents(subFile); //Path to my_date1.html or my_date1.php
echo $new_html_content = str_replace('{TODAYS_DATE}', $today, $subfile_html); // It will replace the occurance {TODAYS_DATE} with today's date
// OUTPUT:
// Hello world, today is June 20 2014
?>
Step2: my_date1.html or my_date1.php
<div> Hello world, today is {TODAYS_DATE} </div>

PHP dynamically generated hit counter

I am a total PHP novice and am trying to write what I think is a pretty simple script. This is the code I have so far:
HTML / PHP
<?php
$hits = file_get_contents('hits.txt');
++$hits;
file_put_contents('hits.txt', $hits);
echo $hits;
$url = $_GET['w'];
?>
<iframe src="<?php echo $url; ?>"></iframe>
<p>
<?php echo $hits; ?>
</p>
The result is a page with an iframe and a hit counter.
The problem with this script is that if the variable $url changes, the hit counter does not. My goal would be that if I visited http://www.website.com/index.php?w=blue.html I would get a different counter than if I visited http://www.website.com/index.php?w=yellow.html.
EDIT: I should add that this script is designed to accept any URL. I realize this complicates things significantly. My ultimate goal would be that if the counter didn't already exist for that particular URL, it would be generated on the fly.
Your current code saves the hit points for every page to the same file as a simple string.
You have a number of options. Here's one that would work if you prefer to stick with text files instead of databases.
You could take the URL in, hash it, and save the counter for that page in a hash-named text file.
Something like
if( isset($_GET) && !empty($_GET['W']) ){
$url = md5($_GET['w']);
$hits = file_get_contents('/hit_counters/'.$url.'.txt');
$hits++;
file_put_contents('/hit_counters/'.$url.'.txt', $hits);
}
and then later you could echo out the hits under that or pull the hits in on another script and echo like that.
If you need it to create new ones on the fly, you could add something like
if(!is_file('/hit_counters/'.$url.'.txt')){
$fh= fopen('/hit_counters/'.$url.'.txt', 'w');
fwrite($fh, '1');
fclose($fh);
}
NOTE
This could end up creating a ton of tiny text files, though. So be aware. If you are worried about that, you would really need to look into a database or read in a text file line by line to find the same hash.
TO IMPLEMENT
Replace the top part of your code within the <?php ?> with the following:
if( isset($_GET) && !empty($_GET['W']) ){
$url = md5($_GET['w']);
if(!is_file('/hit_counters/'.$url.'.txt')){
$fh= fopen('/hit_counters/'.$url.'.txt', 'w');
fwrite($fh, '1');
fclose($fh);
}else{
$hits = file_get_contents('/hit_counters/'.$url.'.txt');
$hits++;
file_put_contents('/hit_counters/'.$url.'.txt', $hits);
}
}
This will take the page name in, hash it, check to see if /hit_counters/THEHASH.txt exists, and create it if not or add +1 to it otherwise. A hash is sort of like encryption, but not really. It will change your $_get['w'] into a longer random-looking string.
You're writing the same hits.txt file regardless of what $_GET['w'] is set to. Try putting the hits.txt file in a folder like this:
<?php
$url = $_GET['w'];
$dir = str_replace(".html", "", $url);
$hits = file_get_contents($dir.'/hits.txt');
++$hits;
file_put_contents($dir.'/hits.txt', $hits);
echo $hits;
?>
<iframe src="<?php echo $url; ?>"></iframe>
<p>
<?php echo $hits; ?>
</p>

Php is not working but iframe is working fine

I have few sets of code to display right side of my webpage. I am using this place to show some user information and some short of ads. For ads I am using that code in my database. Now the problem is that I want to display one external php file which is located in the same directory where I am trying to include or require this php file.
But I dont know why this is not working. I tried with iframe that works fine but I want to use php include but it is not working .please tell where I am doing mistake. Here is my code:
if (is_array($data['blocks'])) {
$output .= '<div id="appside">';
foreach($data['blocks'] as $block) {
if (is_array($block)) {
$output .= '
<div class="block">';
if ($block['title']) {
$output .= '<div class="block_title">'.$block['title'].'</div>';
}
$output .= '<div class="block_content">
'.$block['content'].
'</div>
</div>
';
}
}
$output .= get_gvar('theme_block_sidebar').
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
</div>
';// end of app_sidebar
I tried with all short of alternatives but its printing raw php code. its not getting executed. I tried to put <?php include(\'/home/xxxxxxx/public_html/xxxxxxxa.php\'); ?>
and all other sort of alternatives but its printing raw php code.
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
PHP won't execute inside a string (which is all the above is).
You can't really concatenate an include statement (unless the included file returns something via return). Given I can't tell if your code snippet is inside a function or how it's executed, my best solution is to use output buffering.
$output .= get_gvar('theme_block_sidebar') . '<div>';
ob_start();
include "/home/xxxxxxx/public_html/xxxxxxxa.php";
$output .= ob_get_contents();
ob_end_clean();
$output .= '</div>'
if the file you wish to include is in the same directory as this file, then you should include the filename only
<?php include('filename.php'); ?>
if it is in a higher level or in a directory or a sub-directory in a higher level, then you should move up to this level then down to the file.
<?php include('../../path/to/file.php'); ?>
where ../../ is going up 2 levels, you can set it to as many levels you want to go up.
If you want to get the output from an external php file in a variable, you can get it using the PHP buffering functions:
ob_start();
include("/home/xxxxxxx/public_html/xxxxxxxa.php");
$output_from_php_file = ob_get_contents();
ob_end_clean();
Then you can append $output_from_php_file to your $output variable wherever you want.

PHP: How do I display the contents of a textfile on my page?

I have a .txt file on my web server (locally) and wish to display the contents within a page (stored on the same server) via PHP echo.
The .txt file contains a number that is updated by another script on another page, but for this page I just want to pull the number/txt file contents from the file and echo it (to save the page having to do the calculation involved in getting the number again).
How can I do this?
Here's what I've got so far:
<?php
$myFile = "http://renownestates.com/cache/feedSubscribers.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 1);
fclose($fh);
echo $theData;
?>
Here, try this (assuming it's a small file!):
<?php
echo file_get_contents( "filename.php" ); // get the contents, and echo it out.
?>
Documentation is here.
For just reading file and outputting it the best one would be readfile.
If you aren't looking to do anything to the stuff in the file, just display it, you can actually just include() it. include works for any file type, but of course it runs any php code it finds inside.
I had to use nl2br to display the carriage returns correctly and it worked for me:
<?php
echo nl2br(file_get_contents( "filename.php" )); // get the contents, and echo it out.
?>
I have to display files of computer code. If special characters are inside the file like less than or greater than, a simple "include" will not display them. Try:
$file = 'code.ino';
$orig = file_get_contents($file);
$a = htmlentities($orig);
echo '<code>';
echo '<pre>';
echo $a;
echo '</pre>';
echo '</code>';
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Try this to open a file in php
Refer this: (http://www.w3schools.com/php/showphp.asp?filename=demo_file_fopen)
if you just want to show the file itself:
header('Content-Type: text/plain');
header('Content-Disposition: inline; filename="filename.txt"');
readfile(path);
Use PHP's fopen

Categories