My web server is running on one server, and the data fetching is running on another server. My code will try copy a remote file and store it in my local path. The remote location is, for example, /home/testUser/remoteData.xml. I know the server IP address and hostname. How can I construct the path to fill in the remotePath?
#remotePath is the path to the file on the network
public function __construct()
{
$this->localPath="currentData.xml";
$this->remotePath="a remote path";
}
#this will attempt to make a copy from that network file and store it locally
private function fetchServer()
{
if (!(file_exists($this->remotePath)) || (time() - filectime($this->remotePath)) >= 1200)
return false;
else
{ $success = copy($this->remotePath, $this->localPath);
if($success)
return true;
else
return false;
}
}
Thank you very much!
If you’re using HTTP, you won’t be able to check the creation time. If you know that it is a small, manageable file, you can use file_get_contents:
If it is a bit larger, you can use the cURL and stream syntax, or you can do something similar to what I did above:
Of course, all situations you will have to download the file to a local version and then do some form of checksum to see if there is a difference.
EDIT
You just mentioned that timestamp is a requirement but that you also have SSH. If you have SSH, then you might be able to use SFTP (which is something that piggy-backs off of SSH). I have used phpseclib for several projects and, while not perfect (it is in PHP4 and it does not truly tunnel for SSH), it is a very impressive library nevertheless. Look at the networking part of the manual and (if I remember) it will give you more than enough to do what you’re asking for.