我想编写一个脚本来检查远程服务器上是否存在该文件。
如果文件Write
中的文件存在found.txt
个网址。我如何从txt文件中读取多个网址,并将Write
网址读取到另一个txt文件?
这是我的代码:
<pre>
$sourcePath = "http://www.example.net/file.txt";
$AgetHeaders = @get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
</pre>
答案 0 :(得分:0)
如果您将网址存储在每行一个文本文件中,您可以像这样管理它:
$handle = fopen("AddressList.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$sourcePath = $line;
$AgetHeaders = @get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
}
} else {
// error opening the file.
}
fclose($handle);