【问题标题】:Can't write txt-file on server | not within allowed path(s)无法在服务器上写入 txt 文件 |不在允许的路径内
【发布时间】:2016-08-29 09:14:40
【问题描述】:

我在学校服务器上的私人文件夹中写入文件时遇到问题。我听说问题是我的权利。还是代码?我相信文件夹位置是正确的。我收到一些错误:

1    $testfile = "SchoolServer/Test.txt";
2           
3           if (!file_exists($testfile)) 
4           {
5               $fileoperation = "a";
6               $test = preg_replace( '/\h+/', ' ', $test);
7               $file = fopen ($testfile, $fileoperation);
8               fwrite ($file, $week . "\r\n" . $test . "\r\n" . "\r\n");
9               fclose ($file);
10          }

警告:file_exists():open_basedir 限制生效。文件(Test.txt)不在允许的路径内:第 3 行 D:\Register-test.php 中的 (D:\SitesTemp)

警告:fopen():open_basedir 限制生效。文件(Test.txt)不在允许的路径内:第 7 行的 D:\Register-test.php 中的 (D:\SitesTemp)

警告:fopen(Test.txt):打开流失败:第 7 行的 D:\Register-test.php 中不允许操作

警告:fwrite() 期望参数 1 是资源,布尔值在第 8 行的 D:\Register-test.php 中给出

警告:fclose() 期望参数 1 是资源,布尔值在 D:\Register-test.php 第 9 行给出

谢谢!

【问题讨论】:

  • 你在 $testfile = "SchoolServer/Test.txt" 中使用了什么完整路径;它是完整的路径吗?你试过在你的 php 文件中使用 print_r($_SERVER) 吗?
  • 这是写在 if (!file_exists($testfile)) 中的,目的是检查文件是否存在。如果没有,它会写入它。如果它已经存在,它不会覆盖,而是将内容添加到已经存在的文件中

标签: php


【解决方案1】:

fopen 似乎失败了,你应该测试它返回的内容:

$file = fopen($testfile, $fileoperation);
if ($file) {
    fwrite($file, $week . "\r\n" . $test . "\r\n" . "\r\n");
    fclose($file);
}

另外,好像你的文件位于php打不开的地方,见here

【讨论】:

  • 这不会返回任何东西。该命令也没有错误消息
  • 如果失败,它应该返回一个资源或 false。在使用 fwrite 或 fclose 之前,您需要检查您是否获得了资源。问题似乎来自您的文件所在的位置,尝试将其放在与您的 php 脚本相同的目录中,以查看它是否正常工作。
  • 我尝试将它放在同一个文件夹中。同样的问题。还尝试在我的桌面上编写 txt 文件,但没有运气。
  • 你读过关于 open_basedir 的链接:php.net/manual/en/ini.core.php#ini.open-basedir 吗?
  • 这里有一个针对此错误的故障排除清单:stackoverflow.com/questions/36577020/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
相关资源
最近更新 更多