The fopen function is mostly used to read and write files in PHP, but it is sometimes used to get files from remote servers, but we need to turn on Allow_url_fopen to read remote files using fopen.
Resolution process
The problem with DNS was first ruled out, because except for these functions, everything else worked fine. Although the URL with the domain name is problematic, the gethostbyname () function can be returned correctly. Then came the idea of a php.ini configuration problem--but found that Allow_url_fopen was open. After asking Google for help, someone mentions the problem of SELinux. But I didn't open selinux at all. Continue Google, found the stackoverflow of this article
The code is as follows |
Copy Code |
$file = fopen (' http://www.google.com/', ' RB '); Var_dump (Stream_get_meta_data ($file)); /* Output Result: Array (10) { ["Wrapper_data"]=> Array (2) { ["Headers"]=> Array (0) { } ["Readbuf"]=> Resource (+) of type (stream) } ["Wrapper_type"]=> String (4) "CURL" ["Stream_type"]=> String (4) "CURL" ["Mode"]=> String (2) "RB" ["Unread_bytes"]=> Int (0) ["Seekable"]=> BOOL (FALSE) ["uri"]=> String ("http://www.google.com/") ["Timed_out"]=> BOOL (FALSE) ["Blocked"]=> BOOL (TRUE) ["EOF"]=> BOOL (FALSE) }*/ |
To use functions such as fopen, getimagesize, or include to open a URL, you need to set the php.ini, usually set Allow_url_fopen to on Allow fopen URL, set Allow_url_ Include on allows Include/require URLs, but not necessarily in a local test environment
Allow_url_fopen = On
Whether to allow the treatment of URLs (like http:/or ftp://) as files.
Allow_url_include = On
Whether to allow Include/require to open URLs (like http:/or ftp://) as files.
In the local WAMP test environment, this setting, fopen can open the remote address normally, but encountered local address will be error, for example
The code is as follows |
Copy Code |
1 fopen ("http://localhost/myfile.php", "R");
|
You will get an error after the maximum execution time of the script set in the php.ini, informing the file that it does not exist. This will not occur on the online server, but if localhost is replaced with 127.0.0.1, it will work correctly.
From the situation, the problem is in DNS resolution, it is supposed that localhost has been automatically mapped to 127.0.0.1, actually access http://localhost and access http://127.0.0.1 also reached the same address.
The solution is to check the Windows host file, usually located in the System32 directory, a system disk is the C drive of the host path as shown below
The code is as follows |
Copy Code |
C:/windows/system32/drivers/etc/hosts |
Open the Hosts file and use the tools such as Notepad or notepad++
Remove the 127.0.0.1 in front of the following.
The code is as follows |
Copy Code |
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost |
What is the use of a URL as a file
For example, to send a value to the include file, you can
The code is as follows |
Copy Code |
|
In the example.inc.php
The code is as follows |
Copy Code |
Var_dump ($_get[' foo '); Var_dump ($_get[' bar '); ?> |
Run results
String (1) "1" string (1) "2"
http://www.bkjia.com/PHPjc/632063.html www.bkjia.com true http://www.bkjia.com/PHPjc/632063.html techarticle The fopen function is mostly used to read and write files in PHP, but sometimes it is also used to get files from remote servers, but we need to open allow_url_fopen when using fopen to read remote files ...