PHP Curl check for file existence


a PHP program that downloads a pdf from a backend and save to a local drive.

$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);

    }

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.
Related Posts:
  • PHP identical operator === Variable types are also important in comparison.When you compare two variableswith the identical operator (===), like this, the active types for the zvals are compared,and if they are different, the comparison fails outright… Read More
  • Get mail info with IMAP or POP3 To read mail using IMAP or POP3, which allows you to create a web-based email client. Use PHP's IMAP extension, which speaks both IMAP and POP3: // open IMAP connection $mail = imap_open('{mail.server.com:143}', &nb… Read More
  • PHP-Renaming Files and Directories Renaming  Files and Directories There are a few useful file and directory functions built-in to PHP which I'll cover in brief here. They include renaming and deleting files as well as listing files located in a… Read More
  • Adding CSS style in php script <?php       echo '<span style="font-size:10px">';    // add  styles as  style attribute       echo 'test';     &nbs… Read More
  • Php tutorial - imagemagick Php tutorial - imagemagick Php tutorial - imagemagick - create, edit and compose bitmap  imagemagick is free software  to create, edit, and compose bitmap images in many formats from the commandline or via progra… Read More
  • difference between print and echo - php print( ) is a function, echo is a language construct.  This means that print( ) returns a value, while echo doesn't. You can include print( ) but not echo in larger expressions. echo is  run very fast than  … Read More
  • Displaying Browser Specific-php However, having seen some of the possible values of HTTP_USER_AGENT in the last chapter, you can imagine that there are hundreds of slightly different values. So it's time to learn some basic pattern matching.You'l… Read More
  • php Sessions page Php Sessions are used to help maintain the values of variables across multiple web pages. This is done by creating a unique session ID that is sent to the client browser. The browser then sends the unique ID back on eac… Read More
  • php implode array elements php implode array elements Join array elements with a string string. implode(string, arraypieces) Returns a string containing a string representation of all the array elements in the same order,  string between each e… Read More
  • Send Email from a PHP Script you use the mail() function (in combination with a web form in particular), make sure you check it is called from the desired page and protect the form with a CAPTCHA mail(to,subject,message,headers,parameters); <?php… Read More
  • The $_REQUEST Variable-php PHP is a lot more than a way to work with text. You’ve been working with strings non-stop, but there are a lot more types of information you’ll need to work with in your PHP scripts. As you might expect, there are all kinds … Read More
  • php -Mail Functions The mail() function requires an installed and working email subsystem for sending mail. The program to be used is defined by configuration directives in the php.ini file. A common pitfall is that these are not set up cor… Read More
  • php-use the header() function-php An HTTP (HyperText Transfer Protocol) header is used to send information back and forth between the server and the client (the Web browser). Normally this information is in the form of HTML, which is why the address for Web … Read More
  • Third-party Cookies Third-party Cookies Third-party cookies come from other domain sources that have items, such as ads or images,  embedded on the page adjust cookie and site data permissions. Manage your cookies and site data - chrome … Read More
  • php urlencode - Make a strong GET Query String  php urlencode URL-encodes a string, converting spaces into plus (+ ) signs. urlencode() makes a string safe to use as part of a URL. It does this by encoding every character within the string that may be misi… Read More