Curl-PHP: how to quickly determine whether a remote file exists

Source: Internet
Author: User
My code: {code...} This is not a very stable unified path. sometimes it succeeds and sometimes fails, and the wait time is too long! My code:

// Determine whether the remote file exists if (! Function_exists ('check _ url') {function check_url ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_NOBODY, true); curl_setopt ($ ch, CURLOPT_FAILONERROR, true); curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); $ res = curl_exec ($ ch); if ($ res! = False) {$ statusCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); return $ statusCode;} curl_close ($ ch); return false ;}}

This is not a very stable unified path, sometimes the success or failure, and the waiting time is too long!

Reply content:

My code:

// Determine whether the remote file exists if (! Function_exists ('check _ url') {function check_url ($ url) {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_NOBODY, true); curl_setopt ($ ch, CURLOPT_FAILONERROR, true); curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); $ res = curl_exec ($ ch); if ($ res! = False) {$ statusCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); return $ statusCode;} curl_close ($ ch); return false ;}}

This is not a very stable unified path, sometimes the success or failure, and the waiting time is too long!

Strange requirements: if the remote server is your own, you can simply write an interface on the server to check whether a file exists. If it is not yours, you can only check whether your network and the server-side network are fast and stable. just use your own method to write it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.