网络文件路径

发布于 2024-11-18 11:09:45 字数 741 浏览 3 评论 0原文

我的网络服务器在一台服务器上运行,而数据获取在另一台服务器上运行。我的代码将尝试复制远程文件并将其存储在我的本地路径中。例如,远程位置为 /home/testUser/remoteData.xml。我知道服务器 IP 地址和主机名。如何构建填写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;
   }
}

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-11-25 11:09:45

如果您使用 HTTP,您将无法检查创建时间。如果你知道它是一个小的、可管理的文件,你可以使用 file_get_contents:

 $fl = @file_get_contents( '<stream>' );
 if( !$fl ) return false; // file does not exist remotely
 $ret = @file_put_contents( '<output name>', $fl );// just output it
 return $ret && true; //force it to boolean

如果它有点大,你可以使用 cURL 和 Stream 语法,或者你可以做类似于我上面所做的事情:

 $fl = @fopen( '<stream>' );
 if( !$fl ) return false;// file does not exist remotely
 $out = @fopen( '<local file>', 'w');
 if( !$out ) return false;
 while($data = fread($fl)){fwrite($out,$data);}
 fclose($out);
 return true;

当然,所有情况您必须将该文件下载到本地版本,然后进行某种形式的校验和以查看是否存在差异。

编辑


您刚刚提到时间戳是一个要求,但您也有 SSH。如果您有 SSH,那么您也许可以使用 SFTP(这是基于 SSH 的)。我已经在几个项目中使用了 phpseclib ,虽然并不完美(它是在 PHP4 中,但它并没有真正为SSH),尽管如此,它仍然是一个非常令人印象深刻的库。查看手册的 网络 部分(如果我记得的话)它会给你更多信息足以满足您的要求。

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:

 $fl = @file_get_contents( '<stream>' );
 if( !$fl ) return false; // file does not exist remotely
 $ret = @file_put_contents( '<output name>', $fl );// just output it
 return $ret && true; //force it to boolean

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:

 $fl = @fopen( '<stream>' );
 if( !$fl ) return false;// file does not exist remotely
 $out = @fopen( '<local file>', 'w');
 if( !$out ) return false;
 while($data = fread($fl)){fwrite($out,$data);}
 fclose($out);
 return true;

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.

最单纯的乌龟 2024-11-25 11:09:45

如果您使用 HTTP 并且启用了 PHP allowed_url_fopen 指令,您可以直接使用文件的 URL。示例:

$content = file_get_contents('http://www.yourdomain.dom/virtual/path/to-your-file');

/virtual/path/to-your-file 是文档根目录的相对路径。

如果您的 PHP 环境受限制,不允许打开远程文件,您可以使用 PHP cURL 或 PHP

编辑:

要将文件内容保存到本地文件,请使用 file_get_contents() 的对应部分: file_put_contents()。例子:

file_put_contents('path/to/the/local-file', $content);

If you are using HTTP and PHP allow_url_fopen directive is enabled you can directly use URL for your file. Example:

$content = file_get_contents('http://www.yourdomain.dom/virtual/path/to-your-file');

/virtual/path/to-your-file is relative path to your document root.

If you have a restricted PHP environment that does not allow to open remote files, you can use PHP cURL or PHP streams.

EDIT:

To save the file contents to the local file use the counterpart of file_get_contents(): file_put_contents(). Example:

file_put_contents('path/to/the/local-file', $content);
橙幽之幻 2024-11-25 11:09:45

我不太确定我是否理解你的问题,但如果你的远程位置是

http://www.somehost.com/home/testUser/remoteData.xml

,那么这正是你的路径。如果您想通过 HTTP 获取源代码,则必须以 http:// 开头。如果您想通过 FTP 获取它,请使用 ftp://。这将指示 copy() 函数启动适当的协议

编辑:关于 filectime() 这是一个文件系统函数,不适用于 HTTP 或 FTP。如果您是源文件的作者,您可以在标题中设置一些日期时间信息。

I am not quite sure if I understand your question but if your remote location is

http://www.somehost.com/home/testUser/remoteData.xml

then this is exactly your path. You have to begin with http:// if you like get the source via HTTP. Use ftp:// if you wish to get it with FTP. This will instruct the copy() function to initiated the appropriate protocol

Edit: About filectime() this is a filesystem function and will not work for HTTP or FTP. If you are the author of the source file you may set some date time information into the header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文