当文件名中包含 +(加号)时,PHP file_get_contents() 不起作用

标签 php file-get-contents symbols

我有几个文件名称中带有加号,但我似乎无法打开它们,因为该函数将其解释为空格。

示例:

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/

相关文章:

php - 仅当字符串不为空/空时如何解密字符串

php - 如何使用php从文本文件中获取x个字符数?

java - 在内部类中找不到类型符号

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

php - 添加到脚本 echo file_get_contents(myfile.php) 后,脚本 myfile.php 的某些部分将不起作用。知道为什么吗?

Xcode A+ 源代码控制符号

c++ - 我可以从运行时加载的共享对象访问主机进程的符号吗?还有其他选择吗?

php - 如何停止在子类中调用基类的构造函数

php - JSON POST 请求 - Pocket API/OAuth2.0

php - 简单计划 - 模式数据库