Page 1 of 1

opening a file from another server

Posted: Sat Oct 22, 2005 4:53 am
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.

Posted: Sat Oct 22, 2005 5:11 am
by foobar
It should work if you have permission to access files on that server.

Posted: Sat Oct 22, 2005 5:34 am
by lazersam
Yes I have permission. I own both domains. when I run the file_exists function is comes up negative.

Posted: Sat Oct 22, 2005 5:50 am
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?????

Posted: Sat Oct 22, 2005 8:45 am
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..

Posted: Sat Oct 22, 2005 8:48 am
by lazersam
ok, thanks. do you know how to get the filesize of the txt file if it is placed on anther domain?

Posted: Sat Oct 22, 2005 8:53 am
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..