Using a txt file to display links with php - php

I am trying to display a links using php and txt file.
My Text file (text.txt)
Spiderman, www.spiderman.com
See No Evil, www.seenoevil.com
My php code so far (index.php);
<html>
<head>
<title>Reading from text files</title>
</head>
<body>
<?php
$f = fopen("text.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
// Make an array using comma as delimiter
$arrM = explode(",",fgets($f));
// Write links (get the data in the array)
echo "<li><a href='http://" . $arrM[1] . "'>" . $arrM[0]. "</a></li>";
}
fclose($f);
?>
</body>
</html>
Error I keep getting when I run index.php. This is what the browser displays;

If the browser gives you the output you have mentioned, this means that PHP has not been installed in your machine.Download and install WAMP server to execute php scripts.But, if php has been installed, you are not using the server address, i.e. http://localhost/APP/. file:///C:/server/www/APP/ will not execute the php script.

I think you opened the index.php directly in a browser. There is no error in this code. Try run it in the proper way .that is (localhost/yourDirectory/index.php)

You can use trim() to remove the whitespace and the new line characters, then it should work fine, I just tried:
(Also, make sure the txt file doesn't have the utf-8 BOM)
<html>
<head>
<title>Reading from text files</title>
</head>
<body>
<?php
$f = fopen("text.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
// Make an array using comma as delimiter
$arrM = explode(",",fgets($f));
// Write links (get the data in the array)
echo "<li><a href='http://" . trim($arrM[1]) . "'>" . trim($arrM[0]). "</a></li>";
}
fclose($f);
?>
</body>
</html>

Related

Simple Snippet to read and show content from file

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.

Order of inclusion and interpretation for php and html

I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.

Can't use document.write more than one time in PHP

Here is my HTML and I call external PHP
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<script src="index.php"></script>
<p>My first paragraph.</p>
</body>
</html>
and my PHP Script
<?
$strFileName = "poom.bis";
$objFopen = fopen($strFileName, 'r');
if ($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
// echo $file;
echo "document.writeln('$file'+);";
}
fclose($objFopen);
}
$test = "hello world";
echo "document.writeln(
'<ul>'+
'<li>.$test.</li>'+
'<li>test2</li>'+
'<li>test3</li>'+
'</ul>'
);";
?>
It error when using document.write more than one time
What should I do to solve this problem
Please Advice
PS. use echo "document.writeln('$file'+);"; for one time there is no error and show a result
First error: your line
echo "document.writeln('$file'+);";
should be
echo "document.writeln('$file');";
(without the plus sign). Also make sure that the file poom.bis doesn't contain a newline, not even at the end. If it does, you have to strip them away (trim()).
Second error was (until you edited it) the use of document.writeIn (which doesn't exist) instead of document.writeln (which does).
Tested and it works.
Also, while I'm at it, since you asked for advice how to solve this problem: look at your browser's error console and try to debug it.
echo '<script>document.writeln(';
echo '"<ul><li>test1</li><li>test2</li><li>test3</li></ul>"';
echo ');</script>';
;

PHP write to file minus the PHP code?

I want to write a file to a file and the file contains some PHP code. I don't want the file to run the PHP when someone reads the file. Basically, I want all the text between the <?php and the ?>, plus those tags. Is there any way to do this in PHP? Possibly with strpos? I tried to use strpos; but I couldn't figure it out.
Here's an example:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
The easiest way is probably to parse the file using token_get_all, loop through the result and discard everything that's not of type T_INLINE_HTML.
If your <?php ?> tags are always gonna me at the top of your input file, you could just explode the input and write to your output everything around your tags:
Input:
<?php echo "This is the PHP I want removed!"; ?>
<html>
<p>This is what I want written to a file!</p>
</html>
Code:
$inputTxt = file_get_contents($path . $file , NULL, NULL);
$begin = explode("<?php", $inputTxt);
$end = explode('?>', $inputTxt);
fwrite($output, $begin[0] . $end[1] . "\n\n");
?>
Output:
Before
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
After
<html>
<p>This is what I want written to a file!</p>
</html>
But, if you plan on having more than one set of <?php ?> tags, then you would need to use preg_match:
Input:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>
Code:
<?php
$file="input.txt";
$path='C:\\input\\';
$output = fopen($path . "output.txt",'w');
$inputTxt = file_get_contents($path . $file , NULL, NULL);
$pattern = '/<\?php.+\?>/isU';
$replace = '';
$newInput = preg_replace($pattern, $replace, $inputTxt);
fwrite($output, $newInput);
?>
Output:
Before
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>
After
<html>
<p>This is I want written to a file!</p>
</html>
If you can choose the filename you're writing to, you can write to a .phps file, which won't be evaluated as PHP. If a visitor views the .phps page, they'll be served up a plaintext file that includes everything inside the <?php ?> tags, as well as the HTML.

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