Obi*_*ill 4 php file-get-contents
I recently upgraded my XAMPP from PHP 5.2 to 5.3.1
I seem to be having a problem with file_get_contents().
I can use the function to get something like "http://www.google.com", but it times out when I use it on a domain I have setup locally e.g. "http://localhost/my_dir/my_css_file.css".
我不确定问题是什么.如果这是一个错误,是否有可行的替代方案?
好心提醒.
尝试使用include()而不是file_get_contents().
<?php include($_SERVER['HTTP_HOST'] . "/my_dir/my_css_file.css"); ?>
Run Code Online (Sandbox Code Playgroud)
要么
<?php include($_SERVER['DOCUMENT_ROOT'] . "/my_dir/my_css_file.css"); ?>
Run Code Online (Sandbox Code Playgroud)
更新对应您的评论:
$string = get_include_contents('somefile.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这会将文件数据转换为变量$string.
使用 CURL 解决了这个问题。这是代码。它将与远程文件一起使用,例如 http://yourdomain.com/file.ext
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$file_path_str.'');
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
curl_close ($ch);
Run Code Online (Sandbox Code Playgroud)
我无法使用 @James 解决方案,因为我正在代码中的其他地方使用 @James 解决方案ob_start,ob_flush所以这会让我的事情变得一团糟。
| 归档时间: |
|
| 查看次数: |
16332 次 |
| 最近记录: |