0

I'm using this code now to display a text file on a php/html page.

<?php
foreach (glob("example.txt") as $filename) {
    echo nl2br(file_get_contents($filename));
    echo "<br></br>";
}
?>

I'm looking for a way to display the example.txt file from another server with URI.

Something like this: http://address.com/dir/example.txt

Is there a simple way to do this?

(I would use an iframe but it's not possible to style the text without Java or JQuery).

4

5 回答 5

2

You could just use

file_get_contents('http://address.com/dir/example.txt');
于 2012-11-29T13:55:58.223 回答
0

You will have to use CURL to fetch the content of the file first and then display it.

Another option is to use iframes and set the target of iframe to the desired text file.

Yet another option is to use ajax to fetch the content from client end as suggested in comment.

于 2012-11-29T13:53:34.900 回答
0

Check fopen() / fread() and your available transport wrappers.

于 2012-11-29T13:56:02.683 回答
0

For normal length text file you can use:

 <?PHP
 $file = fopen("http://www.xyz.com/textfile.txt", "rb");
 $output = fread($file, 8192);
 fclose($file);
 echo($output);
 echo "<br>";
 ?>

For longer files:

 <?PHP
 $file = fopen("http://www.xyz.com/textfile.txt", "rb");
 $output = '';
 while (!feof($file)) {
 $output .= fread($file, 8192);
 }
 fclose($file);
 echo($output);
 echo "<br>";
 ?>

It will prevent packet exceed issues in longer files by concatenating the file together in several groupings using while loop

于 2012-11-29T14:03:39.030 回答
0

You code is totally wrong

 foreach (glob("example.txt") as $filename) {
                    ^------------------------- searching for a file 

They can only one example.txt file in a folder at a time except you want to get all text files should should be like this in the first place

 foreach (glob("*.txt") as $filename) { 

If that is not the case the code would work for both remote and local file

error_reporting(E_ALL);
ini_set("display_errors", "on");
$fileName = "example.txt" ; // or http://oursite.com/example.txt 
echo nl2br(file_get_contents($fileName));
echo "<br></br>";
于 2012-11-29T14:07:53.593 回答