How can a time limit be set for file_get_contents in PHP?
To set a time limit for file_get_contents in PHP, you can use the stream_context_create function to create a stream context with a timeout option. This allows you to specify the maximum time in seconds that the function should wait for the file to be fetched before timing out. By setting a timeout value, you can prevent the script from hanging indefinitely if the remote server is unresponsive.
$context = stream_context_create(array(
'http' => array(
'timeout' => 10 // Timeout value in seconds
)
));
$file_contents = file_get_contents('http://example.com/file.txt', false, $context);
Related questions
-
PHP enctype multipart/form-data
-
client IP address PHP redirected domains
-
mail function PHP contact form