PHP Curl check for file existence before downloading

后端 未结 4 715
一整个雨季
一整个雨季 2021-01-19 07:43

I am writing a PHP program that downloads a pdf from a backend and save to a local drive. Now how do I check whether the file exists before downloading?

Currently I

4条回答
  •  灰色年华
    2021-01-19 08:05

    You can do this with a separate curl HEAD request:

    curl_setopt($ch, CURLOPT_NOBODY, true);
    $data = curl_exec($ch);
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    

    When you actually want to download you can use set NOBODY to false.

提交回复
热议问题