Click to See Complete Forum and Search --> : Can a PHP file access a text file on a different domain


Salam
02-18-2003, 08:59 AM
I tried to have my php file read the contents of a text file on another domain but had an error. Is it the case that php files can only read text files on the same server or even same domain.

Thanks,
Salam

nkaisare
02-18-2003, 09:26 AM
Try setting the permissions for the text file at 777. It may work... I've never tried it. In any case, it is a bad way to do things due to security concerns. Is there any reason why you can't have the text file on the same domain?

pyro
02-18-2003, 09:58 PM
Originally posted by Salam
Is it the case that php files can only read text files on the same server or even same domain.Yes

Salam
02-18-2003, 10:48 PM
Hi, actualy searching on the net I found where the problem I had was. You may find this interesting. Its in not being able to get the filesize.

---------------------
This will not work:
---------------------
$filenamee = $contentsFilePath = "http//www......./file.txt";

$file = fopen ($filename, "r");
$contents = fread($file, filesize($filename));

can only handle files on the same domain using a relative path
=============

---------------------
This will work:
---------------------
$filenamee = $contentsFilePath = "http//www......./file.txt";

$file = fopen ($filename, "r");

while(!feof($file)) {
$contents = $contents . fread($file, 1024);
}

can handle files anywhere using a full path
=============

I found this on this page:
http://www.php.net/manual/en/function.fread.php

by a comment from user:
rob at lbox.org
on
14-Nov-2002 02:28
=============

Regards
Salam

pyro
02-18-2003, 10:52 PM
I would wonder if that is a sort of hack, as I don't think that you are supposed to be able to do that... Or maybe I'm thinking of something else right now, I just had a very high fever, so I'm not thinking clearly yet. :p

Salam
02-18-2003, 11:06 PM
Hi, if you are able to see the contents of the text file using its URL in the browser, why should not be able to read it in a program ?

BUT here is the question I have for you:
Is it safe to put the PHP program I wrote in a directory accesable to useres using its URL. In other words is there any way for others to see the class and methods I wrote ? or I should put it on a directory that is not accessable and make another small PHP file that calles the methods in the one including the actual code.

Salam

Salam
02-18-2003, 11:09 PM
Pyro, I can't thank you enough for you help. I did not hear of PHP when I posted my first question on this forum. you made me the basic PHP code that evolved to a 600 line program. I am real lucky that you led me to PHP since it uses Java syntax which I know good.

Salam

pyro
02-18-2003, 11:12 PM
Since PHP is executed server side, it is quite safe to put it in a directory that is accessible on the internet. If users try to download the file, they will simply get the output of the file, and not the file itself.

Yes, PHP is quite easy to pick up if one has knowledge of javascript. It is syntactically quite close, you just need to learn the commands/quirks of the language...

Salam
02-18-2003, 11:24 PM
Hi, actually I ment the Java programing language not Java Script. PHP uses mostly the exact syntacs of Java (not Java Script). There is even a similarity in some of the function libraries like handeling files and to a degree strings :)

pyro
02-18-2003, 11:28 PM
I don't know any Java, but my above statement was correct as well, PHP and javascript are syntactically close. :D It's nice to hear that Java and PHP are close, incase I ever decide to learn Java...

Salam
02-18-2003, 11:32 PM
Hi, the similarity between PHP and Java is far more than the similarity between PHP and Java Script :)

Salam