如何检查外部服务器上是否存在文件

Rol*_*and 7 php

如何检查外部服务器上是否存在文件?我有一个网址" http://logs.com/logs/log.csv ",我在另一台服务器上有一个脚本来检查这个文件是否存在.我试过了

$handle = fopen("http://logs.com/logs/log.csv","r");
if($handle === true){
return true;
}else{
return false;
}
Run Code Online (Sandbox Code Playgroud)

if(file_exists("http://logs.com/logs/log.csv")){
return true;
}else{
return false;
}
Run Code Online (Sandbox Code Playgroud)

这些方法不起作用

kip*_*zes 10

function checkExternalFile($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $retCode;
}

$fileExists = checkExternalFile("http://example.com/your/url/here.jpg");

// $fileExists > 400 = not found
// $fileExists = 200 = found.
Run Code Online (Sandbox Code Playgroud)


Ski*_*ick 3

  1. 确保错误报告已打开。

  2. 使用if($handle)

  3. 检查allow_url_fopen是否为true。

  4. 如果这些都不起作用,请在 file_exists 页面上使用此方法