PHP function: determine whether a remote file exists. example code _ PHP Tutorial

Source: Internet
Author: User
Tags file url
PHP function: determines whether the remote file contains the instance code. This article introduces PHP functions-how to determine whether remote files have instance code? Php * function: remote_file_exists function: determines whether a remote file has a parameter: $ url_file-remote file URL This article introduces the PHP function-determine whether a remote file has an instance code

  1. /*
  2. Function: remote_file_exists
  3. Function: determines whether a remote file exists.
  4. Parameter: $ url_file-remote file URL
  5. Return: If yes, true is returned. if not, false is returned for other reasons.
  6. */
  7. Function remote_file_exists ($ url_file ){
  8. // Check 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. // Obtain 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. // Connect to the 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 the request
  25. Fwrite ($ fp, $ request_str );
  26. $ First_header = fgets ($ fp, 1024 );
  27. Fclose ($ fp );
  28. // Determine whether 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. ?>


Success-determine whether the remote file has the instance code? Php/* function: remote_file_exists function: determines whether a remote file has a parameter: $ 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.