I'd like to read a remote text file (ideally using fopen) using PHP. My script works using fopen when I'm using this function on a local file.
I've tried:
$file = fopen ("http://abc.abc.abc", "r");
if (!$file) {
echo "<p>Unable to open remote file.\n";
exit;
}
and I got:
Warning: fopen(http://abc.abc.abc): failed to open stream: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\NMR\nmrTest5.php on line 2 Unable to open remote file.
I've read that phpseclib could be a good option and since I can access to my files using WinSCP (SFTP) or by using Puttyfor I tried this (after copying all the files from phpseclib to my directory) hoping that I could copy locally the file and then read it with fopen (not the best thing for met but I could live with that):
include('Net/SFTP.php');
$sftp = new Net_SFTP('abc.abc.abc');
if (!$sftp->login('username', 'pass')) {
exit('Login Failed');
}
and I got:
Notice: No compatible server to client encryption algorithms found in C:\xampp\htdocs\NMR\Net\SSH2.php on line 1561 Login Failed
Interstingly, I got a different message if I was connected to the server (using WinSCP):
Notice: Error reading from socket in C:\xampp\htdocs\NMR\Net\SSH2.php on line 3362
Notice: Connection closed by server in C:\xampp\htdocs\NMR\Net\SSH2.php on line 1471 Login Failed
Any idea on how I could get it to work? Ideally I would use fopen but I'm open to other solution.
fopen
orfile
orfile_get_contents
for a URL, you need to enableallow_url_fopen
in thephp.ini
or usingini_set
. Otherwise as the answer given says, use cURL to grab the contents of the URL – Anarthrousfopen()
is very clear: "No connection could be made because the target machine actively refused it". It is not a problem on your code, the remote computer doesn't accept the connection. Can you open the URL (http://abc.abc.abc
) in browser? If you can, thenfopen()
should also be able to open it. If you cannot then maybe the URL is incorrect and it won't open no matter how you try. – Siskset_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
– SchiroElse echo "it works!";
to my code and I get: it works). Now I need to get to my file... I think I'm moving in the right direction. Thanks. – Uriniferous$text = $sftp->get('/path/to/file');
in myelse
statement. I think I'm downloading the file when I do that (which is not ideal) but I can live with that now. Thanks for your help. – Uriniferousget
takes. – Schiro