This article describes PHP functions-determining if a remote file has an instance code
- /*
- Function: remote_file_exists
- Function: Determine whether a remote file exists
- Parameters: $url _file-Remote file URL
- Return: Presence returns true, no presence or other reason returned false
- */
- function remote_file_exists ($url _file) {
- Detection 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";
- Connecting to a 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 ";
- $request _str. = "Host:". $host. " ";
- $request _str. = "Connection:close";
- Send Request
- Fwrite ($fp, $request _str);
- $first _header = fgets ($fp, 1024);
- Fclose ($FP);
- Determine if a 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.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 ...