PHP fopen: some insights on reading URLs with Chinese characters

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.