【问题标题】:file_get_contents doesn't work [closed]file_get_contents 不起作用[关闭]
【发布时间】:2015-03-12 00:29:59
【问题描述】:
$z = file_get_contents("vars.txt");
var_dump($z);

这是我当前的代码。当我运行它时,我看到了这个输出:

string(0) ""

我不知道为什么 - 这个文件包含 321 个字符。它与我的 PHP 脚本位于同一目录中。你能解释一下原因吗,为什么这不起作用?写入此文件正常(使用“w+”模式)。

虽然这是chmod的错误,但我写了chmod 777 dir命令,还是不行。

我的脚本的完整代码在那里:https://gist.github.com/ty221/274bda4ecec03f710691

显示所有错误 E_ALL 不起作用 - 我的意思是什么都没有显示。

【问题讨论】:

  • 这是你的全部剧本吗?
  • 不...我会粘贴它
  • 在打开<?php标签error_reporting(E_ALL); ini_set('display_errors', 1);后立即将错误报告添加到文件顶部
  • @JayBlanchard 没有任何改变
  • 你愿意认为@Ty221,但不能保证任何外部来源。

标签: php file error-handling


【解决方案1】:

如何阅读手册:http://php.net/manual/en/function.fopen.php

还有一段话:

'w+' 开放读写;将文件指针放在文件的开头并将文件截断为零长度。如果文件不存在,请尝试创建它。
^^^^^^^^^^^^

这意味着你的线路:

//Deletes all content in the file
$file = fopen("vars.txt", "w+");

$to_save = "";

//Reads the content from a empty file
$z = file_get_contents("vars.txt");
//Outputs an empty string
var_dump($z);

【讨论】:

  • 很好。这就是为什么op在提问时需要编写完整的代码
  • 哇,你的眼光真好
猜你喜欢
  • 1970-01-01
  • 2012-08-30
  • 2013-06-30
  • 2014-11-16
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多