However, a problem occurred when reading an image yesterday. Later, it was found that the URL contains Chinese characters.
For example:Copy codeThe Code is as follows: $ files = fopen ('HTTP: // www.website.com/my pp.jpg', 'rb ');
The returned value of "$ files" will be "False ". First of all, I thought of using urlencode to encode the URL and found that the URL still does not work. The original urlencode will encode the ":" and "/" characters, so the URL is not a URL. Well, if you say something, replace the encoding of the ":" and "/" characters.Copy codeThe Code is as follows: $ url = 'HTTP: // www.website.com/my pp.jpg ';
$ Url = preg_replace ('/\ % 3A/I', ':', preg_replace ('/\ % 2F/I ','/', urlencode (urldecode ($ url); $ file = fopen ($ url, 'rb ');
Try, hey ~ It's okay. Next, let's review the fopen () function:
The fopen () function opens a file or URL. If opening fails, this function returns FALSE. Open successfully. This function returns TRUE.
I. Syntax:
Copy codeThe Code is as follows: fopen (filename, mode, include_path, context)
Parameters |
Description |
Filename |
Specifies the file or URL to open. |
Mode |
Specifies the access type of the file/stream. Possible values are shown in the table below. |
Include_path |
If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE. |
Context |
Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. |
Ii. Possible values of the mode parameter:
Mode |
Description |
"R" |
Open the file in read-only mode and point the file pointer to the file header. |
"R +" |
Open in read/write mode and point the file pointer to the file header. |
"W" |
Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
"W +" |
Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
"" |
Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
"A +" |
Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
"X" |
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
"X +" |
Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |