Whenever I vày this on a certain server, the result is empty. When I vì it anywhere else, the result is whatever the page"s nội dung may be. When I however, on the hệ thống where the result is empty, use the function locally - without accessing an external URL (file_get_contents("../simple/internal/path.html");), it does work.Bạn sẽ xem: Php file_get_contents() function

Now, I am pretty sure it has something to bởi with a certain php.ini configuration. What I am however not sure about is, which one. Please help.

Bạn đang xem: Hàm file_get_contents() trong php

php url external file-get-contents ini cốt truyện Improve this question Follow edited Aug 15 "10 at 17:45

Patrick McElhaney 52.3k3737 gold badges123123 silver badges157157 bronze badges asked Aug 15 "10 at 17:23

arikarik 23.1k3434 gold badges9191 silver badges146146 bronze badges địa chỉ a phản hồi |

8 Answers 8

Active Oldest Votes42The setting you are looking for is allow_url_fopen.

You have two ways of getting around it without changing php.ini, one of them is khổng lồ use fsockopen(), và the other is khổng lồ use cURL.

I recommend using cURL over file_get_contents() anyways, since it was built for this.

giới thiệu Improve this answer Follow answered Aug 15 "10 at 17:24

AillynAillyn 20.8k2121 gold badges5555 silver badges8181 bronze badges 3 địa chỉ a comment | 39Complementing Aillyn"s answer, you could use a function like the one below to mimic the behavior of file_get_contents:

function get_content($URL) $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $URL); $data = curl_exec($ch); curl_close($ch); return $data;echo get_content("http://example.com"); chia sẻ Improve this answer Follow edited Aug 8 "15 at 22:44


maxisme 3,32644 gold badges3535 silver badges7878 bronze badges answered Aug 15 "10 at 18:26

You should be aware that enable that option may make some bugs in your code exploitable.

Xem thêm: File Excel Bị Lỗi Has Stopped Working ” Trên Windows 10

For instance, this failure lớn validate input may turn into a full-fledged remote code execution vulnerability:

copy($_GET, "."); mô tả Improve this answer Follow answered Aug 15 "10 at 17:24 ArtefactoArtefacto 90k1515 gold badges186186 silver badges215215 bronze badges add a bình luận | 4$ch = curl_init();curl_setopt($ch, CURLOPT_URL, "http://www.your_external_website.com");curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);$result = curl_exec($ch);curl_close($ch);is best for http url,But how to open https url help me

nội dung Improve this answer Follow edited Dec 29 "14 at 7:12 Alireza Fallah 4,39133 gold badges2727 silver badges5454 bronze badges answered Feb 28 "13 at 9:55 VikashVikash 58355 silver badges1212 bronze badges địa chỉ a bình luận | 2The answers provided above solve the problem but don"t explain the strange behaviour the OP described. This explanation should help anyone testing communication between sites in a development environment where these sites all reside on the same host (and the same virtualhost; I"m working with apache 2.4 & php7.0).

There"s a subtlety with file_get_contents() I came across that is absolutely relevant here but unaddressed (probably because it"s either barely documented or not documented from what I can tell or is documented in an obscure php security model whitepaper I can"t find).

With allow_url_fopen set khổng lồ Off in all relevant contexts (e.g. /etc/php/7.0/apache2/php.ini, /etc/php/7.0/fpm/php.ini, etc...) và allow_url_fopen set khổng lồ On in the command line context (i.e. /etc/php/7.0/cli/php.ini), calls to file_get_contents() for a local resource will be allowed và no warning will be logged such as:


// Path outside document root that webserver user agent has permission khổng lồ read. E.g. For an apache2 webserver this user agent might be www-data so a tệp tin at /etc/php/7.0/filetoaccess would be successfully read if www-data had permission to read this filefile_get_contents("");or

// Relative path in same document rootfile_get_contents("data/filename.dat")To conclude, the restriction allow_url_fopen = Off is analogous to an iptables rule in the output chain, where the restriction is only applied when an attempt khổng lồ "exit the system" or "change contexts" is made.

N.B. Allow_url_fopen set to lớn On in the command line context (i.e. /etc/php/7.0/cli/php.ini) is what I had on my system but I suspect it would have no bearing on the explanation I provided even if it were set to Off unless of course you"re testing by running your scripts from the command line itself. I did not test the behaviour with allow_url_fopen set to lớn Off in the command line context.