-1

注: これはパフォーマンスに関する質問です

mysql データベースに 20,000 の画像 URL があり、画像 URL が有効で壊れていないかどうかを確認するために 1 分間隔で cron を実行しています。EC2 small で実行しています。@GetImageSize などのメソッドを試して、ヘッダーと cURL をチェックしましたが、ジョブに最大 10 分かかります。画像をダウンロードする必要がなく、非常に高速な方法があるかどうかを知りたいです。

ループ内の約25枚の画像に対する以下の提案(クレジットと称賛)からのいくつかのテストを次に示します。

function method2($link){                               //45sec
    if (@GetImageSize($link)) {
        echo  "image exists ";
    } 
}

function method4($url){                            //13 sec
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_exec($ch)!==FALSE)    {
            echo  "image exists ";
    }
}


function method3($filename){                          //20sec
    $h = fopen($filename, 'r');
    if ($h !== false) {
        echo 'File exists';
            fclose($h);
    }
}

function method5($url){                             //21 sec 
    if(@file_get_contents($url,0,NULL,0,1)){
        echo "image exists";
    }
}

function method6($url){                             //22 sec
    if (false === file_get_contents($url,0,null,0,1)) {
        echo "no ";
    }
}

function method1($url){                                //13 sec
    exec("wget --spider -v ".$url);
}
4

2 に答える 2

1

allow_url_fopenホストで「オン」になっている場合はfopen、URL を読み取って、何も読み取らずに閉じることができます。

$h = fopen('http://www.example.com/img.jpg', 'r');
if ($h !== false) {
    echo 'File exists';
    fclose($h);
else {
    echo 'File does not exist';
}

あなたはターゲット サーバーの所有者と連絡を取り合っているように見えるので、おそらくまったく別のアプローチを取る必要があります。ファイル システムに存在するファイルのリストを返す、リモート サーバーでホストするスクリプトを呼び出します。次に、このスクリプトを最後から呼び出します。これは、毎分 20,000 件のリクエストで目標を達成しているため、いずれにせよ望ましいことです。

于 2013-07-10T10:35:02.923 に答える
0

「file_exists」のようなphp関数を使用できます。詳細については、そのリンクをたどってください http://php.net/manual/en/function.file-exists.php

またはこれを使用します

$file = 'http://www.abc.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}
于 2013-07-10T10:34:12.013 に答える