create a variable from a url

I have a file ‘quotes.php’ (code below) that calls up a random quotation from a separate text file called ‘quotes.txt’. It works fine. But… I’d like to have just one ‘quotes.txt’ at a specific url, say http://www.mysite.com/quotes.txt’ where it can be accessed through ‘quotes.php’ on several different sites hosted on different servers.

I suspect I simply need to change the line “$file = “quotes.txt”;” to make the location a url, but I do not know how to do this. Thanks for some assistance!

<?
$file = "quotes.txt"; // Alter if not in server root

// Do not edit below

$x = 1;
if (file_exists($file)) {
	$text = fopen($file, "r");
		if ($text) {
			while(!feof($text)) {
			$str[$x] = fgets($text);
			$x++;
			}
		}
	fclose($text);
	}
	else {
		die("Data file does not exist");
	}
$top = $x-3;
$ct = rand(0, $top);
if ($ct==0) { 
	$ct=1; 
	}
if (!$str[$ct]) {
	while (!$str[$ct]) {
		$ct = rand(0, $top);
		if ($ct==0) { $ct=1; }
		}
	}
echo "<b> <i>$str[$ct]</i></b>";
?>

have a look at the fsockopen and related commands. I think that might be the ticket you need.

[php]$file = “http://www.mysite.com/quotes.txt”;[/php]

Thank you peg110 and Q1712 for the replies! I’m not trying to get someone to do this for me, I just don’t know where to look to get answers…

I checked into " fsockopen and related commands" but that is way too complicate for me yet, so I tried

$file = "http://www.mysite.com/quotes.txt";

and

$file = "www.mysite.com/quotes.txt";

and for both I get the error message from line 18 of my original code above

		die("Data file does not exist");

Could something in my on my host’s server or in my php.ini file need to be reconfigured to allow php to access files &/or urls? A newbie pal reports he is using this same two files as me with

$file = "http://www.mysite.com/quotes.txt";

and it works fine on his hosts server and he did not need to change php.ini or have his host reconfigure anything.

my fault: file_exists() dosn’t work with URL’s

so u just have to modyfy ur if:
[php]if (file_exists($file)) {
$text = fopen($file, “r”);
…[/php]
to
[php]if ($text = @fopen($file, “r”)) {
…[/php]

Hey, Q1712,

As my pals Larry, Moe and Curly say: “Success.” “Success.” “Soitenly!” That worked perfectly, thank you SO much! I appreciate your time and indulgence.

If you look in the next day or so, you can check out my now-working test file here but I’ll be deleting it soon.

The original code that I adapted came from Kev’s Place. So thanks, Kev, too.

Thanks for the credit. :) I still can’t see why the alteration from a local file (in $file) to a remote file didn’t work. As I said in discussions with you, on my site (and server) the change worked flawlessy.

Sponsor our Newsletter | Privacy Policy | Terms of Service