0

短篇故事:

我在我的 php 脚本中遇到了 open_basedir 限制 - 一个简单的“将新文件写入磁盘的测试”脚本。在我看来,我的 open_basedir 设置正确并且文件位于正确的位置 - 但没有运气,每次都是同样的错误。我在此站点上搜索了类似的 open_basedir 问题,但我没有看到任何与此问题有关的问题 - 目录看起来正确但仍然抛出错误。

我对问题的猜测是:
1)open_basedir 不能像我认为的那样工作
2)我的设置错误,我只是没有看到它
3)实际上是别的东西,比如 IIS 读/写权限,等
4) ???

很长的故事:

我正在使用 PHP 处理 IIS 服务器,并且正在尝试使以下代码片段正常工作(一个简单的写入文件测试):

date_default_timezone_set('America/Chicago');
$myFile = 'testfile.txt';
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Some Text\n";
fwrite($fh, $stringData);
$stringData = "Some More Text\n";
fwrite($fh, $stringData);
fclose($fh);

这个 php 脚本位于我服务器上的 C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php

这是我对 open_basedir 的 php.ini 设置:
open_basedir = c:\inetpub\wwwroot;c:\inetpub\wwwroot\WEBDIRECTORY

每当我打开脚本页面时,我都希望看不到任何输出,然后当我回来查看时,应该有一个新文件写入服务器。这就是我得到的:

警告:fopen():open_basedir 限制生效。文件(testfile.txt)不在允许的路径内:第 5 行 C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php 中的 (c:\inetpub\wwwroot) 警告:fopen(testfile.txt): failed打开流:第 5 行的 C:\inetpub\wwwroot\WEBDIRECTORY\test_write.php 中不允许的操作无法打开文件

我尝试了很多排列:最初 open_basedir 行只是
open_basedir = c:\inetpub\wwwroot

......但这也不起作用。

我还尝试输入 testfile.txt 的绝对路径(c:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt 等),而不仅仅是文件本身的名称,并且我不断收到相同的错误消息。

有任何想法吗?非常感谢你的帮助。

4

2 回答 2

1

Okay, so sorry to waste everyone's time - turns out it was a server setting my system administrator had set for me. (I'm new to this so I'll explain in general terms that I understand)

He'd set it up so that the server would not resolve an address for any files in the web directory except certain types you expect to find on a website, such as .htm, .html, .php, .jpg, etc. The file I was trying to read was .txt.

So, I wrote some test code:

$test = realpath('c:\inetpub\wwwroot\WEBDIRECTORY\testfile.php');
echo ("test = $test\n");
if($test == FALSE){
 echo ("... is FALSE\n");
}

$test = realpath('c:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt');
echo ("test = $test\n");
if($test == FALSE){
 echo ("... is FALSE\n");
}

Which promptly returned this:

test = C:\inetpub\wwwroot\WEBDIRECTORY\testfile.php
test = ... is FALSE

So, realpath is refusing to return an address for anything ending in .txt, but is more than happy to do that for a .php file. This means that WHENEVER I put in an otherwise legal (within the basedir) filename that ends in an illegal extension, the server resolves that address not as "C:\inetpub\wwwroot\WEBDIRECTORY\testfile.txt" but as "". And of course, an empty string is not going to be in the basedir, returning open_basedir restriction errors.

BIZARRE! But makes sense now that I finally traced it back to its source. Thanks for all the tips! It helped me go through process of elimination and figure this one out.

于 2010-10-27T14:48:45.267 回答
-1

这似乎是一个权限问题。检查 C:\inetpub\wwwroot\WEBDIRECTORY\ 的权限 如果需要特殊权限,有时您必须更改 .PHP 脚本的权限以进行写入。通常,您需要目录和脚本集,而不仅仅是其中一个。

这是 PHP 手册中的一个很好的脚本,用于打开文件进行写入:

$filename = 'test.txt'; $somecontent = "添加到文件中\n";

// 让我们首先确保文件存在并且是可写的。if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
     echo "Cannot open file ($filename)";
     exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else { echo "文件 $filename 不可写"; }

于 2010-10-25T20:41:12.517 回答