我有几个文件名称中带有加号,但我似乎无法打开它们,因为该函数将其解释为空格。
示例:
File name: Report_Tue-Jun-02-2015-14:11:04-GMT+0200-(W.-Europe-Daylight-Time).html
当我尝试打开它时:
Warning: file_get_contents(/cores/Report_Tue-Jun-02-2015-14:11:04-GMT 0200-(W.-Europe-Daylight-Time).html) [function.file-get-contents]: failed to open stream: No such file or directory in .... on line 150
这是我的代码:
$file = $_GET['FILE'];
$file = str_replace('+', '%2B', $file);
$content = file_get_contents($file);
有什么想法/解决方案吗?
最佳答案
以下方法对我来说效果很好。
<?
# File name: Report_Tue-Jun-02-2015-14:11:04-GMT+0200-(W.-Europe-Daylight-Time).html
# Method 1.
$file = 'Report_Tue-Jun-02-2015-14:11:04-GMT+0200-(W.-Europe-Daylight-Time).html';
$data = file_get_contents($file);
print($data);
# Method 2.
$data = file_get_contents('Report_Tue-Jun-02-2015-14:11:04-GMT+0200-(W.-Europe-Daylight-Time).html');
print($data);
?>
关于当文件名中包含 +(加号)时,PHP file_get_contents() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612685/