我做了一个函数来检测一个有效的文件,但是当与foreach集成时,结果变得不准确
准确
$urlpath = 'http://' .$domain.'/'.$txtfile;;
if (!file_exists($urlpath)) {
echo 'The file ' . $txtfile . ' does not exist on ' .$domain. '<br/>';
}
由于foreach
变得不准确:
foreach($domainlist as $domain) {
$urlpath = 'http://' .$domain.'/'.$txtfile;;
if (!file_exists($urlpath)) {
echo 'The file ' . $txtfile . ' does not exist on ' .$domain. '<br/>';
}
}
file_exists()
只能使用本地路径而不是URL。您正在尝试确定上面的URL是否是一个文件,请使用php is_file()
file_exists
() 适用于本地/服务器文件。如果您想通过 URL 进行检查,例如,如果与 200
不同或404
,您可以验证响应 http 代码。
function url_file_exists($url)
{
$ch = curl_init($url);
curl_exec($ch);
$exists = (!curl_errno($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200);
curl_close($ch);
return $exists;
}
if (!url_file_exists($urlpath)) { // $urlpath = http://www.example.com/file.txt
echo 'File not exists.';
}