This article introduces the PHP function-determine whether the remote file exists instance code
- /*
- Function: remote_file_exists
- Function: Determine whether the remote file exists
- Parameters: $url_file - Remote file URL
- Return: returns true if it exists, false if it does not exist or for other reasons
- */
- function remote_file_exists($url_file){
- //Detect 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 server
- $fp = fsockopen($host, $port, $err_no, $err_str, 30);
- if (!$fp){ return false; }
- "Host: ".$host." ";
- $request_str .= "Connection: Close ";
-
- //Send request
- fwrite ($fp, $request_str);
- $first_header = fgets($fp, 1024);
- fclose($fp);
-
- / /Determine whether the file exists
- if (trim($first_header) == ""){ return false; }
- if (!preg_match("/200/", $first_header)){
- return false;
- }
- return true;
- }
- //Test code
- $str_url = http://www.ite5e.com/newsinfo.php?nid=1493;
- $exits = remote_file_exists($str_url);
- echo $exists ? "Exists" : "Not exists";
- ?>
-
-
-
-
http://www.bkjia.com/PHPjc/486160.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/486160.htmlTechArticleThis 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...