title: PHP Curl check for file existence before downloading
body: <p>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? </p>
  
  <p>Currently I am using curl (see code below) to check and download but it still downloads the file which is 1KB in size.</p>
  
  <pre><code>$url = "http://wedsite/test.pdf";
  $path = "C:\\test.pdf;"
  downloadAndSave($url,$path);
  
  function downloadAndSave($urlS,$pathS)
      {
          $fp = fopen($pathS, 'w');
  
          $ch = curl_init($urlS);
  
          curl_setopt($ch, CURLOPT_FILE, $fp);
          $data = curl_exec($ch);
  
          $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          echo $httpCode;
          //If 404 is returned, then file is not found.
          if(strcmp($httpCode,"404") == 1)
          {
              echo $httpCode;
              echo $urlS; 
          }
  
          fclose($fp);
  
      }
  </code></pre>
  
  <p>I want to check whether the file exists before even downloading. Any idea how to do it?</p>