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
-
- /*
- Function: remote_file_exists
- Function: determines whether a remote file exists.
- Parameter: $ url_file-remote file URL
- Return: If yes, true is returned. if not, false is returned for other reasons.
- */
- Function remote_file_exists ($ url_file ){
- // Check 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 ;}
-
- // Obtain request data
- $ Host = $ url_arr [host];
- $ Path = $ url_arr [path]. "? ". $ Url_arr [query];
- $ Port = isset ($ url_arr [port])? $ Url_arr [port]: "80 ";
-
- // Connect to the 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 the request
- Fwrite ($ fp, $ request_str );
- $ First_header = fgets ($ fp, 1024 );
- Fclose ($ fp );
-
- // Determine whether 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 ";
- ?>
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...