简体   繁体   English

如何修改此代码以检查远程主机上是否存在文件?

[英]How do I modify this code to check for file existence on a remote host?

I'm attempting to check for the existence of multiple files on a single website using the code below and am encountering a problem that only the top URL is being tested and even if it is a valid URL I still get URL doesn't exist 我正在尝试使用以下代码在单个网站上检查多个文件的存在,并且遇到一个问题,即仅对顶部URL进行了测试,即使它是有效的URL,我仍然得到的URL不存在

How would I modify the code to correctly return the results and check all the given url's within the text-file. 我将如何修改代码以正确返回结果并检查文本文件中所有给定的url。

<?php 
$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$found = false;
foreach($urls as $url)
   if($_POST['url'] == $site . $url)
      $found = true;

if($found)
   echo "URL exists";
else
   echo 'URL doesn\'t exist';

?>

A little bit of a logic change - customise to your needs. 逻辑上的一点变化-根据您的需求进行定制。

<?php 

$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url)

   // TEST URL EXISTENCE HERE (not sure if just looking at $_POST will tell you if its a remote url?

   if($_POST['url'] == $site . $url) {
       echo "URL exists";
   } else {
       echo 'URL doesn\'t exist';
   }

}

?>

Try this. 尝试这个。 Note you may have to skip line endings, so use rtim(). 请注意,您可能必须跳过行尾,因此请使用rtim()。 Also if you want the urls.txt to test against multiple Input urls, the script will accomplish that as well. 另外,如果您希望urls.txt针对多个输入url进行测试,则脚本也将完成此操作。

<?php 
$site = "http://site.com"
$urls = file('urls.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url) {
  //if you want to test multiple input urls, they might be in input array, say url[]
  //we can check for the array here
  if(is_array($_POST['url'])) {
    foreach($_POST['url'] as $post_url) {
      //You may want to skip line endings, so use rtrim
      if($post_url == ($site . rtrim($url)) {
        print 'Url found - '.$post_url.'<br>';
      } else {
        print 'Url not found - '.$post_url.'<br>';
      }
    }
  } else {
    //You may want to skip line endings, so use rtrim
    if($POST['url'] == ($site . rtrim($url)) {
      print 'Url found - '.$POST['url'].'<br>';
    } else {
      print 'Url not found - '.$POST['url'].'<br>';
    }
  }  
}
?>

Code that will check a list of urls on a remote server: 将检查远程服务器上的URL列表的代码:

<?php 
$site = "http://site.com"
$urls = file('urls.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($urls as $url) {
  $headers = get_headers($site . $url, 1);
  $status_parts = explode(" ", $headers[0]);
  $status_code = $status_parts[1];
   if ($status_code == 200)
     echo "URL exists";
   else if ($status_code == 404)
     echo 'URL doesn\'t exist';
   else
     // error or something else?
}
?>

A couple of things to note: 需要注意的几件事:

  1. There are similar questions 还有类似的问题
  2. You might want to log the url int eh response instead of just outputting whether or not it exists or not. 您可能想记录url int eh响应,而不只是输出是否存在。

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

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