简体   繁体   English

使用PHP检查文件是否在远程服务器上

[英]Check with PHP if file is on remote server

I'm basically looking for an if statement: 我基本上正在寻找一个if语句:

if (....) { } 如果(....){}

else { } 其他{}

...that will check if the current file is on a remote server (online, on my website, rather than in my local configuration with Xampp or Mamp). ...这将检查当前文件是否在远程服务器上(在线,在我的网站上,而不是在我的Xampp或Mamp本地配置中)。

I've done some research already, and it brought me to file_exists , but I'm not sure if this is what I want or if there's a better way to do this. 我已经进行了一些研究,并将其带到file_exists ,但是我不确定这是否是我想要的,或者是否有更好的方法来做到这一点。

I'm not sure if it's relevant why I'm doing this, but here goes: I have a couple different configurations in a functions.php page, and I want it to act differently when it's in production, and when it's in test-mode locally. 我不知道这是否是相关的,为什么我这样做,但在这里有云:我有一个页面的functions.php几个不同的配置,我想它采取不同的行动时,它的生产,而当它在测试-本地模式。

There are a few conditions that have been left out of your question, such as whether the file is even callable via an HTTP request, and whether you are concerned with if the file is the same on the remote server. 您的问题中遗漏了一些条件,例如,是否甚至可以通过HTTP请求调用该文件,以及是否担心该文件在远程服务器上是否相同 If all you care about it existence of the file, and it is exposed via HTTP, you can make an HTTP request for the file, and check the response code. 如果您关心文件的存在,并且已通过HTTP公开,则可以对文件发出HTTP请求,并检查响应代码。 You may want to put a little handling logic in the file to handle these requests. 您可能需要在文件中添加一些处理逻辑来处理这些请求。 If the file isn't accessible via an HTTP request, there are a couple of more options: 如果无法通过HTTP请求访问该文件,则有两个其他选项:

1) Create a script just to check for the file. 1)创建一个脚本来检查文件。 This will take some extra work to make it secure. 这将需要一些额外的工作才能使其安全。 2) FTP into the server and try to download the file. 2)通过FTP进入服务器,然后尝试下载文件。

With either solution, if you desire to know if the file is the same in both places, you can use an MD5 hash on both ends to find out. 无论使用哪种解决方案,如果您都想知道文件在两个地方是否相同,则可以在两端使用MD5哈希来找出。

如果文件将位于其他主机上,则可以使用gethostbyaddr检查,并将其与您的localhost或服务器匹配,然后再跟相应的操作语句进行匹配。

You could use filectime that returns FALSE if the file is remote. 如果文件是远程文件,则可以使用filectime返回FALSE。

$remote = empty(@filectime($url));
if ($remote) {
  // File is remote.
}
else {
  // File is local.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM