PHP Curl get server response [duplicate]

<strong>Possible Duplicate:</strong> Easy way to test a URL for 404 in PHP?

I am using curl to download a sequence of pdfs from the backend in PHP. I do not know when the sequence ends like 1.pdf, 2.pdf......etc.

Below is the code I am using to download pdf:

$url = 'http://<ip>.pdf'; $path = "C:\\test.pdf"; $fp = fopen($path, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); fclose($fp);

But how do I determine that that pdf do not exists in the backend? Does curl returns any response when file not found?

Thanks In Advance


$ch=curl_init("www.example.org/example.pdf"); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $result=curl_exec($ch); curl_close($ch);

with curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); it will return the result on success, FALSE on failure.

with curl_setopt($ch,CURLOPT_RETURNTRANSFER,false); it will return TRUE on success or FALSE on failure.

Moreover, for file not found:

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if($http_code == 404) { /*file not found*/ }


Just test the statut code of the http response after $data = curl_exec($ch);

$http_status = curl_getinfo($url, CURLINFO_HTTP_CODE); if($http_status == '404') { //not found, -> file doesnt exist in your backend }

More info here


  • uploading with SFTP : CURLOPT_SSLKEY is not used
  • PHP CURLOPT_WRITEFUNCTION doesn't appear to be working
  • Curl returns Failed connect to :80
  • PHP | Get private IP address from a client user?
  • curl - how to go to the next page?
  • PHP cURL into authenticated site when already logged in
  • In my CURL CURLOPT_PROGRESSFUNCTION callback, dltotal is always 0
  • elasticsearch: term query fails
  • Proper way to get file size in C
  • whats the maximum value of start parameter when communicating with google's search api in php?
  • cURL call with a port in the url/location not working - couldn't connect to host
  • Functional Programming in Swit to distribute array elements to correct “buckets”
  • SWIG ImportError: undefined symbol: _Py_RefTotal
  • FCM on app closed
  • curl_init() has been disabled for security reasons
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • How to resolve docker host names (/etc/hosts) in containers
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Cannot resolve symbol 'MyApi'
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Circular dependency while pushing http interceptor
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal