【问题标题】:Not able to Create file usind PHP?无法使用 PHP 创建文件?
【发布时间】:2014-03-01 12:34:52
【问题描述】:

所以我正在做的是从文件中创建一个带有名称的目录,然后在其中创建一个文本文件。

我的目录结构是这样的,我正在尝试创建文件:

/users/用户名/www/practice

代码是:

$path = /home/用户名/www/practice/

$counter_file = "/home/username/www/practise/counter.txt";

if (file_exists($counter_file)) {

第 2 行:if(!($fp = fopen($counter_file, "r")))

die("无法打开文件");

          $counter = (int) fread($fp, 20);
          fclose($fp);

          $counter = $counter + 1 ;
      $path .= $counter

          mkdir($path , 0777);
          $path .= content.txt;

          if (!($fp = fopen($path, "w"))) 
          {
              die("cannot open the file");

          }
          fwrite($fp, "{$content}");
  // content is the variable that has value equal to "file is created"
          fclose($fp);

          if(!($fp = fopen($counter_file, "w")))
              die("cann't open the file");

          fwrite($fp, $counter);
          fclose($fp);            // and store the counter value in the file

          if(!($fp = fopen("$counter_file", "w")))
              die("cann't open the file");

          fwrite($fp, $counter);
          fclose($fp);            

}

我得到的错误是:

警告:fopen(counter.txt):打开流失败:第 2 行权限被拒绝

无法打开文件

【问题讨论】:

  • 试试/practise/counter.txt
  • 还是同样的错误@putvande
  • 你有写权限吗?
  • practise 目录有什么权限
  • 练习目录的权限为777

标签: php permission-denied


【解决方案1】:

也许您对目录有权限,但对您的文件没有权限。对您的 txt 文件使用 chmod(777)。 请记住,设置 777 并不是最好的选择。

【讨论】:

  • 我不明白你的要求。而不是 777 只读取文件?
  • 有没有办法以777权限的写入模式打开文件?
  • 我猜不出来。您必须使用 chmod() 函数来设置权限。
猜你喜欢
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多