opening a file from another server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

opening a file from another server

Post by lazersam »

Hi all,

I am trying to open a file that is placed on ANOTHER domain. Does anyone know how to link to it? I have tried

Code: Select all

$filename = "http://www.domain_name.com/files/category1.txt";
						
			
			$file = fopen($filename, "r");
			$filesize = filesize($filename);
			$text = fread($file, $filesize);
			fclose($file);
			
			echo("File Size: $filesize bytes");
			echo ( "<pre>$text</pre>");
This obviously doesn't work.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

It should work if you have permission to access files on that server.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Yes I have permission. I own both domains. when I run the file_exists function is comes up negative.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

The STRANGE thing is when I run this script

Code: Select all

$filename = "http://www.domain_name.com/files/category1.txt";
						
			if (file_exists($filename)) {
			    echo "yes exists.<br>";
			}else{
				echo "no does not exist.<br>";
			}
			
			
			if (is_readable($filename)) {
			    echo "yes readable.<br>";
			}else{
				echo "no not readable.<br>";
			}
			
			if (is_executable($filename)) {
			    echo "yes executable.<br>";
			}else{
				echo "no not executable.<br>";
			}
			
			
			$file = fopen($filename, "r");
			$text = fread($file, 112);
			fclose($file);
			
			echo("File Size: $filesize bytes");
			echo ( "<pre>$text</pre>");
I get this output....
no does not exist.
no not readable.
no not executable.
File Size: bytes
1,this is a test link,site name,description stuff here
2,this is a test link2,site name2,description stuff here2
So the script denies the file is there and YET echo's the file anyhows?????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is_readable(), is_writable(), is_executable(), filesize(), among several other functions require access to the file system. Files on another server are not in the local machine's file system. They are called stat() based functions, this is why they fail..
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

ok, thanks. do you know how to get the filesize of the txt file if it is placed on anther domain?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strlen() of file_get_contents(). If you run PHP 5, you could use get_headers() first to determine if the content-length header is sent, but you'd still have to get the file itself if it wasn't..
Post Reply