1

When I use the PHP file system function fopen to read a file from remote server in the following way:

<?php $filetoread = fopen ('https://www.abc.com/directory/file.php', 'r')  ?>

I get the error

 [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized.....

After reading through several posts like this one https://stackoverflow.com/a/3335437 and PHP manual http://www.php.net/manual/en/function.fopen.php I come to know about it's resolution but because of inadequate information I can't get through the resolution properly.

For example, in the following resolution

http://user:password@www.domain.com/somefile.php, what and how the username and / or password should be added? Are these the username & password of ftp server? Is the resolution applicable for https:// too or fsockopen() shall be better than fopenin such case?

Community
  • 1
  • 1
Klanto Aguntuk
  • 719
  • 1
  • 17
  • 44
  • The username and password are the ones you would have typed in response to the browser's authentication dialogue if you went to the URL by hand. – Barmar May 08 '14 at 21:07
  • @Barmar, I assume that you are saying about the .`htaccess` username & password those we use to protect the directory. Thanks. – Klanto Aguntuk May 09 '14 at 04:32
  • That's one way to do it. Passwords can also be checked in server scripts. – Barmar May 09 '14 at 15:02

1 Answers1

0

PHP fopen manual suggests to use ftp wrapper in this case instead of http:// or https:// and most often these wrappers are kept disabled by the host.

Thus,

$filetoread variable should be pointed to ftp address on the contrary of http or https like following code.

$filetoread = fopen("ftp://user:password@www.abc.com/directory/file.php", "r");

Therefore,

the username are and password should be the username & password of ftp address

Henceforth,

the demonstration can be as following:

$filetoread = fopen("ftp://your ftp username here : your ftp password here@www.abc.com/directory/file.php", "r");
Klanto Aguntuk
  • 719
  • 1
  • 17
  • 44