Home  >  Article  >  Backend Development  >  PHP function: Determine whether the remote file exists instance code_PHP tutorial

PHP function: Determine whether the remote file exists instance code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:41:19671browse

This article introduces the PHP function-determine whether the remote file exists instance code

  1. /*
  2. Function: remote_file_exists
  3. Function: Determine whether the remote file exists
  4. Parameters: $url_file - Remote file URL
  5. Return: returns true if it exists, false if it does not exist or for other reasons
  6. */
  7. function remote_file_exists($url_file){
  8. //Detect 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. //Connect to server
  18. $fp = fsockopen($host, $port, $err_no, $err_str, 30);
  19. if (!$fp){ return false; }
  20.                                                                                                                                         "Host: ".$host." ";
  21. $request_str .= "Connection: Close ";
  22. //Send request
  23. fwrite ($fp, $request_str);
  24. $first_header = fgets($fp, 1024);
  25. fclose($fp);
  26. / /Determine whether the file exists
  27. if (trim($first_header) == ""){ return false; }
  28. if (!preg_match("/200/", $first_header)){
  29. return false;
  30. }
  31. return true;
  32. }
  33. //Test code
  34. $str_url = http://www.ite5e.com/newsinfo.php?nid=1493;
  35. $exits = remote_file_exists($str_url);
  36. echo $exists ? "Exists" : "Not exists";
  37. ?>
http://www.bkjia.com/PHPjc/486160.html
www.bkjia.com

true

TechArticleThis article introduces the PHP function - determine whether the remote file exists instance code?php /* Function: remote_file_exists Function: determine the remote file Whether parameters exist: $url_file - remote file URL...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn