Questions tagged [file-get-contents]

In PHP, file_get_contents is the preferred way to read the contents of a file into a string. It will use memory mapping techniques (if supported by your OS) to enhance performance.

In PHP, file_get_contents is used to read an entire file into a single string. Depending on the server configuration, it can also be used to read URLs as well. It differs from file(), in that file is designed to return a resource pointer and then used read the file in chunks (i.e. CSV files). Use file for large files that should not be read all at once. Use file_get_contents to read smaller files and files that cannot be read as easily read in chunks (i.e. JSON)

For more details see the PHP manual entry.

2607 questions
365
votes
17 answers

How can I handle the warning of file_get_contents() function in PHP?

I wrote a PHP code like this $site="http://www.google.com"; $content = file_get_content($site); echo $content; But when I remove "http://" from $site I get the following warning: Warning: file_get_contents(www.google.com) …
Waseem
  • 11,741
  • 15
  • 41
  • 45
330
votes
3 answers

How to post data in PHP using file_get_contents?

I'm using PHP's function file_get_contents() to fetch contents of a URL and then I process headers through the variable $http_response_header. Now the problem is that some of the URLs need some data to be posted to the URL (for example, login…
Paras Chopra
  • 4,029
  • 4
  • 21
  • 19
273
votes
21 answers

file_get_contents(): SSL operation failed with code 1, Failed to enable crypto

I’ve been trying to access this particular REST service from a PHP page I’ve created on our server. I narrowed the problem down to these two lines. So my PHP page looks like this:
Joe
  • 8,251
  • 3
  • 18
  • 23
186
votes
6 answers

Does file_get_contents() have a timeout setting?

I am calling a series of links using the file_get_contents() method in a loop. Each link may take more than 15 minutes to process. Now, I worry about whether PHP's file_get_contents() has a timeout period? If yes, it will time out with a call and…
Flora Clinton
  • 1,871
  • 2
  • 11
  • 3
122
votes
4 answers

PHP cURL vs file_get_contents

How do these two pieces of code differ when accessing a REST API? $result = file_get_contents('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url'); and $ch =…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
118
votes
11 answers

How to get file_get_contents() to work with HTTPS?

I'm working on setting up credit card processing and needed to use a workaround for CURL. The following code worked fine when I was using the test server (which wasn't calling an SSL URL), but now when I am testing it on the working server with…
James Simpson
  • 13,488
  • 26
  • 83
  • 108
111
votes
16 answers

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get: Warning:…
undefined
  • 5,190
  • 11
  • 56
  • 90
83
votes
8 answers

file_get_contents when url doesn't exist

I'm using file_get_contents() to access a URL. file_get_contents('http://somenotrealurl.com/notrealpage'); If the URL is not real, it return this error message. How can I get it to error gracefully so that I know that the page doesn't exist and…
sami
  • 7,515
  • 8
  • 33
  • 37
79
votes
11 answers

file_get_contents() Breaks Up UTF-8 Characters

I am loading a HTML from an external server. The HTML markup has UTF-8 encoding and contains characters such as ľ,š,č,ť,ž etc. When I load the HTML with file_get_contents() like this: $html = file_get_contents('http://example.com/foreign.html'); It…
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
53
votes
7 answers

Show image using file_get_contents

how can I display an image retrieved using file_get_contents in php? Do i need to modify the headers and just echo it or something? Thanks!
Belgin Fish
  • 19,187
  • 41
  • 102
  • 131
52
votes
12 answers

Replace a whole line where a particular word is found in a text file

How can I replace a particular line of text in file using php? I don't know the line number. I want to replace a line containing a particular word.
kishore
  • 1,017
  • 3
  • 12
  • 21
50
votes
2 answers

Upload a file using file_get_contents

I realise I can do this with CURL very easily, but I was wondering if it was possible to use file_get_contents() with the http stream context to upload a file to a remote web server, and if so, how?
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
50
votes
6 answers

Alternative to file_get_contents?

$xml_file = file_get_contents(SITE_PATH . 'cms/data.php'); The problem is that a server has URL file-access disabled. I cannot enable it, its a hosting thing. So the question is this. The data.php file generates xml code. How can I execute this…
ComputerUser
  • 4,828
  • 15
  • 58
  • 71
49
votes
5 answers

Get file content from URL?

When I use following URL in browser then it prompt me to download a text file with JSOn content. https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json (Click above URL see downloaded file content) Now I want to…
Awan
  • 18,096
  • 36
  • 89
  • 131
48
votes
8 answers

PHP ini file_get_contents external url

I use following PHP function: file_get_contents('http://example.com'); Whenever I do this on a certain server, the result is empty. When I do it anywhere else, the result is whatever the page's content may be. When I however, on the server where the…
arik
  • 28,170
  • 36
  • 100
  • 156
1
2 3
99 100