PHP fopen read a point of view with Chinese URL address

  • 2020-05-26 07:55:09
  • OfStack

However, there was a problem when reading a picture yesterday, and it was found that there were Chinese characters in URL.

For example:
 
$files = fopen('http://www.website.com/ my PP.jpg', 'rb'); 

The return value of "$files" will be "False". The first thing that comes to my mind is to encode URL with urlencode 1, but it still doesn't work. urlencode will encode the characters ":" and "/", so URL will not be URL. Oh, that's a bit of a mouthful, so just replace the ":" and "/" characters with the ones that should work.
 
$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 it. Hey, that's good. Now let's review the fopen() function again:

The fopen() function opens a file or URL. If the opening fails, this function returns FALSE. On success, this function returns TRUE.

1. Grammar:

fopen(filename, mode, include_path, context) 


parameter describe filename Specify the file to open or URL. mode Specify the type of access required to the file/stream. The possible values are shown in the table below. include_path If you also need to retrieve files in include_path, you can set this parameter to 1 or TRUE. context Specifies the environment for the file handle. Context is a set of options for modifying the behavior of a stream.

2. Possible values of the mode parameter:

mode instructions "r" With read-only mode open, point the file pointer to the file header. "r+" With read-write mode open, point the file pointer to the file header. "w" Write mode is turned on, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it. "w+" With read-write mode open, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it. "a" Write mode is turned on to point the file pointer to the end of the file. If the file does not exist, try to create it. "a+" With read-write mode open, point the file pointer to the end of the file. If the file does not exist, try to create it. "x" Create and open as write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE with 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 tag for the underlying open(2) system call.
This option is supported by PHP 4.3.2 and later, and can only be used for local files. "x+" Create and open it as a read/write, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE with 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 tag for the underlying open(2) system call.
This option is supported by PHP 4.3.2 and later, and can only be used for local files.

Related articles: