PHP function: Determine if a remote file exists instance code _php tutorial

Source: Internet
Author: User
This article describes PHP functions-determining if a remote file has an instance code

  1. /*
  2. Function: remote_file_exists
  3. Function: Determine whether a remote file exists
  4. Parameters: $url _file-Remote file URL
  5. Return: Presence returns true, no presence or other reason returned false
  6. */
  7. function remote_file_exists ($url _file) {
  8. Detection input
  9. $url _file = Trim ($url _file);
  10. if (Empty ($url _file)) {return false;}
  11. $url _arr = Parse_url ($url _file);
  12. if (!is_array ($url _arr) | | empty ($url _arr)) {return false;}
  13. GET Request data
  14. $host = $url _arr[host];
  15. $path = $url _arr[path]. "?". $url _arr[query];
  16. $port = Isset ($url _arr[port])? $url _arr[port]: "80";
  17. Connecting to a server
  18. $fp = Fsockopen ($host, $port, $err _no, $err _str, 30);
  19. if (! $fp) {return false;}
  20. Construct the Request protocol
  21. $request _str = "GET". $path. " http/1.1 ";
  22. $request _str. = "Host:". $host. " ";
  23. $request _str. = "Connection:close";
  24. Send Request
  25. Fwrite ($fp, $request _str);
  26. $first _header = fgets ($fp, 1024);
  27. Fclose ($FP);
  28. Determine if a file exists
  29. if (Trim ($first _header) = = "") {return false;}
  30. if (!preg_match ("/200/", $first _header)) {
  31. return false;
  32. }
  33. return true;
  34. }
  35. Test code
  36. $str _url = http://www.ite5e.com/newsinfo.php?nid=1493;
  37. $exits = remote_file_exists ($str _url);
  38. Echo $exists? "Exists": "Not Exists";
  39. ?>

http://www.bkjia.com/PHPjc/486160.html www.bkjia.com true http://www.bkjia.com/PHPjc/486160.html techarticle This article introduces PHP functions-to determine if a remote file exists an instance code? PHP/* Function: remote_file_exists function: Determine whether the remote file exists parameters: $url _file-remote file URL ...

  • 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.