Why fopen url is unavailable in the local wamp test environment

Source: Internet
Author: User
Tags ftp

Problem Description

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, after this setting, fopen can open the remote address normally, but an error will be reported in case of a local address, such

 

The code is as follows: Copy code
Fopen ("http: // localhost/myfile. php", "r ");

An error is reported after the maximum execution time of the script set in php. ini is exceeded, indicating that the file does not exist. This won't happen on the online server, but it works normally if you replace localhost with 127.0.0.1.

According to the situation, the problem lies in DNS resolution. It is said that localhost has been automatically mapped to 127.0.0.1. In fact, accessing http: // localhost and accessing http: // 127.0.0.1 also reach the same address.

The solution is to check the Windows host file, which is usually located in the system32 directory. A system disk is the host path of drive C as follows:

C:/Windows/System32/drivers/etc/hosts

Open the hosts file and use notepad, notepad ++, and other tools.

Remove the # above 127.0.0.1.

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, you can pass a value to an include file.

The code is as follows: Copy code

<? Php include 'http: // yourdomain.com/
Example. inc. php? Foo = 1 & bar = 2';?>
In example. inc. php


<? Php
Var_dump ($ _ GET ['foo']);
Var_dump ($ _ GET ['bar']);
?>

Running result

String (1) "1" string (1) "2"

Add

Fopen cannot create a Chinese file name.

Previously, the chartset on the web page used UTF-8 and the file also used UTF-8. Then, the problem arises when you use fopen () to create a file with a Chinese file name. The file names are all garbled!

I checked a lot of documents and tried a lot of methods to solve the problem. I was thinking about using other methods to bypass this problem. Suddenly, my mind flashed the default Windows text encoding is ansi, and then I ran baidu again, I confirmed this, so my webpage should also be ansi encoded so that the created file name will not be garbled.

Then I started to verify that the web page was saved in ansi, and the chartset statement was removed. It was OK, but the content of the web page became garbled. Later I remembered that this web page also included other web pages, change the include web page to ansi for saving. Haha everything is OK.

Programming really depends on accumulation. If I have never seen that the default Windows code is ansi, then I don't know how many months can this problem be solved.

Ps: <meta content = "text/html; charset = utf-8 "http-equiv =" Content-type "> this meta tag must be placed before <title> </title>.

Later, I came up with a better solution. The webpage still uses UTF-8 encoding and storage, but the parameter numbers of file names in fopen () can be coded separately, php has the iconv () code change program. Convert UTF-8 to gb2312 to avoid garbled Chinese file names.

Test.htm

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta content = "text/html; charsets = UTF-8" http-equiv = "Content-Type">
<Title> title: {title} </title>
</Head>
<Body>
<B> content of the news: </B> {content}
</Body>
</Html>

Test. php

 

The code is as follows: Copy code

<? Php

// The actual application may be to query the database to retrieve the content.
$ Rows = array ("replace Title 1", "Replace content 1"), array ("replace Title 2", "Replace content 2 "));
$ Filename = "tmp.htm ";
Foreach ($ rows as $ id => $ val ){
$ Title = $ val [0];
$ Content = $ val [1];
$ Pagename = "test". $ id. ". html ";
// Encode the file name to avoid garbled Chinese file names
$ Pagename = iconv ("UTF-8", "GBK", $ pagename );
       
// Read the template
$ Tmpfile = fopen ($ filename, "r ");
$ String = fread ($ tmpfile, filesize ($ filename ));
$ String = str_replace ("{title}", $ title, $ string );
$ String = str_replace ("{content}", $ content, $ string );
Fclose ($ tmpfile );
// Write a new file
$ Newpage = fopen ($ pagename, "w ");
Fwrite ($ newpage, $ string );
Fclose ($ newpage );
       
    }
Echo "created successfully! ";
?>

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.