FILE_GET_CONTENTS TO READ FILE IN PHP

In PHP, you can read the nội dung via the function file_get_contents or fopen. However, by default, there is no time out setting for these two functions. So if the target URL takes long time khổng lồ return the file contents, the PHP process may seem frozen. You could set a time out to lớn these two functions so that it will return once the time limit exceeded.

The key function lớn use is the stream_context_create, which creates a context and stores the parameters to pass lớn these two functions.

file_get_contents_with_timeout

The following function returns the nội dung of the URL with a timeout setting:


Bạn đang xem: File_get_contents to read file in php

123456

function file_get_contents_with_timeout($path, $timeout = 30) $ctx = stream_context_create(array("http"=> array("timeout" => $timeout) )); return file_get_contents($path, false, $ctx);

function file_get_contents_with_timeout($path, $timeout = 30) $ctx = stream_context_create(array("http"=> array("timeout" => $timeout) )); return file_get_contents($path, false, $ctx);
*
php


fopen_with_timeout

Similarly, you can pass the context of timeout to the fopen function, and it becomes:


123456

function fopen_with_timeout($path, $timeout = 30) $ctx = stream_context_create(array("http"=> array("timeout" => $timeout) )); return fopen($path, "rb", false, $ctx);

Xem thêm: How Can I Find The Impact Factor And Rank Of A Journal? ? Scimago Journal & Country Rank

function fopen_with_timeout($path, $timeout = 30) $ctx = stream_context_create(array("http"=> array("timeout" => $timeout) )); return fopen($path, "rb", false, $ctx);
382 wordsLast Post: The C++ Windows Command Line Tool to lớn Wait and Timeout on CreateProcess Next Post: How to lớn Compute the power nguồn of Arbitrary Base without Loops in C/C++?

Leave a Reply Cancel reply

Notice: It seems you have Javascript disabled in your Browser. In order khổng lồ submit a comment to this post, please write this code along with your comment: 1a7aee7f0bf831a01b09d234e65b9823


*
Free $25 credit when you sign up with Vultr Cloud VPS (10 Months Giveaway!)Free $20 credit when you sign up with Linode Cloud VPS (4 Months Giveaway!) - (Promotional Code: PodcastInIt2018)Free $10 credit when you sign up with DigitalOcean Cloud VPS (2 Months Giveaway!)Free $10 credit when you sign up with Aliyun Cloud VPS (2 Months Giveaway!)Get more promotion giao dịch by using VPS Database và VPS search Tool

*
Coding For Speed


This tên miền is hosted proudly on QuickHostUK & Namesilo Domain
*
Free $25 credit when you sign up with Vultr Cloud VPS (10 Months Giveaway!)Free $20 credit when you sign up with Linode Cloud VPS (4 Months Giveaway!) - (Promotional Code: podcastinit2018)Free $10 credit when you sign up with DigitalOcean Cloud VPS (2 Months Giveaway!)Free $10 credit when you sign up with Aliyun Cloud VPS (2 Months Giveaway!)Get more promotion giao dịch by using VPS Database và VPS search Tool