7

これはNOの山全体を吐き出しますが、画像はそこにあり、によって表示されるのでパスは正しい<img>です。

foreach ($imageNames as $imageName) 
{
    $image = 'http://path/' . $imageName . '.jpg';
    if (file_exists($image)) {
        echo  'YES';
    } else {
        echo 'NO';
    }
    echo '<img src="' . $image . '">';
}
4

5 に答える 5

16

file_existsURL ではなく、ローカル パスを使用します。

解決策は次のとおりです。

$url=getimagesize(your_url);

if(!is_array($url))
{
     // The image doesn't exist
}
else
{
     // The image exists
}

詳しくはこちらをご覧ください

また、( 関数を使用して) 応答ヘッダーを探すget_headersことは、より良い代替手段です。応答が 404 かどうかを確認するだけです。

if(@get_headers($your_url)[0] == 'HTTP/1.1 404 Not Found')
{
     // The image doesn't exist
}
else
{
     // The image exists
}
于 2013-02-11T05:06:24.073 に答える
15

file_exists は、「http://」URL ではなく、ローカル パスを探します

使用する:

$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
于 2013-02-11T05:04:51.930 に答える
2
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode==200) echo 'YES';
else              echo 'NO';
于 2013-02-11T05:11:59.323 に答える
1

これが私がしたことです。ファイルにアクセスできない場合、必ずしも「404 Not Found」とは限らないため、ヘッダーを取得することで起こりうる結果をより多くカバーしています。場合によっては、「Moved Permanently」、「Forbidden」、およびその他の可能なメッセージです。ただし、ファイルが存在し、アクセスできる場合は「 200 OK 」です。HTTP の部分は、その後に 1.1 または 1.0 を持つことができます。これが、すべての状況で信頼性を高めるために strpos を使用した理由です。

$file_headers = @get_headers( 'http://example.com/image.jpg' );

$is_the_file_accessable = true;

if( strpos( $file_headers[0], ' 200 OK' ) !== false ){
    $is_the_file_accessable = false;
}

if( $is_the_file_accessable ){
    // THE IMAGE CAN BE ACCESSED.
}
else
{
    // THE IMAGE CANNOT BE ACCESSED.
}
于 2016-01-10T00:15:28.860 に答える
0
function remote_file_exists($file){

$url=getimagesize($file);

if(is_array($url))
{

 return true;

}
else {

 return false;

}

$file='http://www.site.com/pic.jpg';

echo remote_file_exists($file);  // return true if found and if not found it will return false
于 2013-02-11T05:41:02.327 に答える