Is there any way to get a simple php code that just displays the text from a txt document, that's on a url?
This is what I've got so far. I'm sure I'm missing something~
<?
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
fopen ($text)
?>
Guessing you can't open it, since it's not on the drive. Any workaround that, or fix you guys could help me with?
Thanks (:
Too easy! :)
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
Explanation:
file_get_contents() will return the contents of the remote file and assign it to the $text variable. Then you'll just have to ouput these contents using the echo statement
Alternative: use the readfile() function. It will output the contents of the file directly:
readfile('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
You don't have to open it. It's already appended as a string to the variable $text.
$text = file_get_contents('https://dl.dropboxusercontent.com/u/28653637/Snip/Snip/Snip.txt');
echo $text;
//Outputs "“Why Don't You Get A Job?” ― The Offspring"
Related
I need to check if a webpage outside of my site has a specific word on it. I’ve tried file_get_contents() but it doesn’t return anything. Is there any way I can do this in PHP?
edit: Here’s what I’ve tried:
$query = 'example';
$file = "https:// www.site.com/search?q=$query";
// tested url and it works, had to add space to post it
$contents = file_get_contents($file);
echo $contents;
I was expecting it to just output the entire page for me to use .includes() on later but it just doesn’t output anything.
Look into curl to get the contents of a web page. Then you can use preg_match to find the word.
I am trying to write a function in which a string of text is written with timestamps to the file "text2.txt", I want each entry to be on a new line, but PHP_EOL does not seem to work for me.The strings simply write on the same line and does not write to a new line for each string.
Could anyone give me some pointers or ideas as to how to force the script to write to a new line every time the function is activated?
Some sort of example would be highly appreciated.
Thank you in advance.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['sendmsg']))
{
writemsg();
}
function writemsg()
{
$txt = $_POST['tbox'];
$file = 'text2.txt';
$str = date("Y/m/d H:i:s",time()) . ":" . $txt;
file_put_contents($file, $str . PHP_EOL , FILE_APPEND );
header("Refresh:0");
}
?>
Also, I want to get rid of the character count on the end of the string when using the below code :
<?php
echo readfile("text2.txt");
?>
Is there any way for the character count to be disabled or another way to read the text file so it does not show the character count?
Could anyone give me some pointers or ideas as to how to force the script to write to a new line every time the function is activated? Some sort of example would be highly appreciated.
Given the code you posted I'm pretty sure newlines are properly appended to the text lines you are writing to the file.
Try opening the file text2.txt on a text editor to have a definitive confirmation.
Note that if you insert text2.txt as part of a HTML document newlines won't cause a line break in the rendered HTML by the browser.
You have to turn them into line break tags <br/>.
In order to do that simply
<?php
echo nl2br( file_get_contents( "text2.txt" ) );
?>
Using file_get_contents will also solve your issue with the characters count display.
A note about readfile you (mis)used in the code in your answer.
Accordind to the documentation
Reads a file and writes it to the output buffer.
[...]
Returns the number of bytes read from the file. If an error occurs, FALSE is returned and unless the function was called as #readfile(), an error message is printed.
As readfile reads a file and sends the contents to the output buffer you would have:
$bytes_read = readfile( "text2.txt" );
Without the echo.
But in your case you need to operate on the contents of the file (replacing line breaks with their equivalent html tags) so using file_get_contents is more suitable.
To put new line in text simply put "\r\n" (must be in double quotes).
Please note that if you try to read this file and output to HTML, all new line (no matter what combination) will be replaced to simple space, because new line in HTML is <br/>. Use nl2br($text) to convert new lines to <br/>'s.
For reading file use file_get_contents($file);
I'm trying to get file content from a php file.
$content = file_get_contents('test.php');
var_dump($content);
//outputs
addUser();
test.php
<?php
$client->addUser();
Why is file_get_contents removing $client-> ?
It's probably not removing anything, but you're reading the result in a browser, and the browser thinks that anything between a < and a > is a html tag.
Use the "view source" function in your browser to see the whole thing.
You've got 2 HTML brackets <>
Try this
echo htmlentites($content);
I've just been fiddling around with PHP, and I want to know if it's possible to do such a thing.
If so, can you please provide example code?
Update: What I mean, is I want to encode whatever file may be at the URI, not the URI itself. Sorry if I'm not clear.
You can put:
base64_encode(file_get_contents('http://www.google.com'));
I don't see why not...
<?php $file = file_get_contents("http://example.com/myfile.txt");
$encoded = base64_encode($file);
file_get_contents function will grab the HTML of given URL and base64_encode function will encode it.
<?php
$content = file_get_contents("http://google.com");
$encodedContent = base64_encode($content);
?>
You want to base64-encode a URL? Simple...
base64_encode($uri)
readfile() says it outputs the content but I want the content to be saved in a variable. How do I do that? I tried $content=readfile("file.txt") but that doesn't come out right.
I want to do like this:
$data=readfile("text.txt");
echo htmlentities($data);
That should give you an idea of how I want that to work.
That is what file_get_contents is for:
$data = file_get_contents("text.txt");
echo htmlentities($data);
If you must use readfile you can use output buffering to get the contents into a variable:
ob_start();
readfile("text.txt");
$data = ob_get_clean();
echo htmlentities($data);
I don't see why you would avoid file_get_contents in this situation though.
readfile() writes the contents of the file to a buffer. Use file_get_contents() instead.