Determine whether remote and non-remote files exist in thinkphp

Source: Internet
Author: User
Tags file url
In thinkphp, you can determine whether a remote or non-remote file exists. * function: remote_file_exists function: determines whether a remote file has a parameter: $ url_file-remote file URL $ flag-true is used to determine a remote file. if false is used to determine whether a non-remote file is returned, true is returned. If no remote file exists, or whether a remote or non-remote file exists in thinkphp
// Determine whether a remote file exists/* function: remote_file_exists function: determine whether a remote file exists. the parameter: $ url_file-remote file URL $ flag-true is used to determine the remote file, if the value is false, the system returns true if a non-remote file exists. if the file does not exist or otherwise, the system returns false */static function remote_file_exists ($ url_file, $ flag = true) {if ($ flag = true) {// The default value is to judge the remote file // Check the input $ url_file = trim ($ url_file); if (empty ($ url_file )) {return false;} $ url_arr = parse_url ($ url_file); if (! Is_array ($ url_arr) | empty ($ url_arr) {return false;} // Get request data $ host = $ url_arr ['host']; $ path = $ url_arr ['path']. "? ". $ Url_arr ['query']; $ port = isset ($ url_arr ['port'])? $ Url_arr ['port']: "80"; // connect to the server $ fp = fsockopen ($ host, $ port, $ err_no, $ err_str, 30); if (! $ Fp) {return false;} // Construct the request protocol $ request_str = "GET ". $ path. "HTTP/1.1 \ r \ n"; $ request_str. = "Host :". $ host. "\ r \ n"; $ request_str. = "Connection: Close \ r \ n"; // send the request fwrite ($ fp, $ request_str); $ first_header = fgets ($ fp, 1024 ); fclose ($ fp); // Determine whether the object exists if (trim ($ first_header) = "") {return false;} if (! Preg_match ("/200/", $ first_header) {return false;} return true;} else if ($ flag = false) {// non-remote file return file_exists ($ url_file );}}

?

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.