open_basedir限制在Plesk for Windows中生效


open_basedir restriction in effect in Plesk for Windows

我正在用PHP开发CMS作为学习练习,但遇到了一个名为"open_basedir restriction"的障碍——我正在尝试上传一个小的JPG文件。我试着尽可能简明扼要地提供信息,但如果我忘记了什么,请告诉我!

我可以看到它每次都会碰到c:''windows/temp/folder,所以它只有在尝试执行move_uploaded_file操作时才会崩溃。

经过大量研究,我知道这是什么,理论上如何修复它,因为我在网上阅读了许多页面,如:

http://forum.parallels.com/showthread.php?258036-Plesk-Windows-open_basedir-restriction-in-effect

我的代码

$uiq = uniqid();
$image_folder = "/img/articles/original/";
$uploaded = false;
if(isset($_POST['upload_image'])){ 
    if($_FILES['userImage']['error'] == 0 ){
        $up = move_uploaded_file($_FILES['userImage']['tmp_name'],  $image_folder.$_FILES['userImage']['name']);
        if($up){
        $uploaded = true;   
        }
    }
}

我的PHPINFO

我的PhpInfo结果显示,我的网络托管空间的根在允许的文件夹列表中:

open_basedir:F:''PLESK''WWW''mydomain.com''httpdocs''

错误

PHP警告:move_uploaded_file():open_basedir限制生效。文件(/img/articles/original/test.jpg)不在第40行F:''PLESK''WWW''mydomain.com''httpdocs''sparklyphp''cms''modules''articles''edit''photos''index.php中允许的路径内

更多错误

如果我更改路径

$image_folder = "/img/articles/original/";

$image_folder = "img/articles/original/";

我得到额外的错误:

PHP Warning:  move_uploaded_file(): open_basedir restriction in effect. File(C:'Windows'Temp'php393F.tmp) is not within the allowed path(s): (F:'PLESK'WWW'mydomain.com'httpdocs') in F:'PLESK'WWW'mydomain.com'httpdocs'sparklyphp'cms'modules'articles'edit'photos'index.php on line 40
PHP Warning:  move_uploaded_file(): open_basedir restriction in effect. File(C:'Windows'Temp'php393F.tmp) is not within the allowed path(s): (F:'PLESK'WWW'mydomain.com'httpdocs') in F:'PLESK'WWW'mydomain.com'httpdocs'sparklyphp'cms'modules'articles'edit'photos'index.php on line 40
PHP Warning:  move_uploaded_file(C:'Windows'Temp'php393F.tmp): failed to open stream: Operation not permitted in F:'PLESK'WWW'mydomain.com'httpdocs'sparklyphp'cms'modules'articles'edit'photos'index.php on line 40
PHP Warning:  move_uploaded_file(): Unable to move 'C:'Windows'Temp'php393F.tmp' to 'img/articles/original/test.jpg' in F:'PLESK'WWW'mydomain.com'httpdocs'sparklyphp'cms'modules'articles'edit'photos'index.php on line 40

**宿主环境**网站托管环境是一个Windows 2008 R2盒子,Plesk 11.5(最新版本/更新)在FastCGI模式下运行PHP 5.4.16。我对整个服务器拥有完全的管理员访问权限。

这里最令人沮丧的是,文件正在上传到临时文件夹,我就是无法从那里获得它!

任何帮助都将不胜感激!

Bob

我不知道为什么会这样。好的,所以最后我通过抓取和存储当前工作目录并将工作目录切换到站点的根目录来解决这个问题:

$storeOriginalPath = getcwd();
chdir($_SERVER['DOCUMENT_ROOT']);

执行上传:

    $uiq = uniqid();
    $image_folder = "img/articles/original/";
    $uploaded = false;
    if(isset($_POST['upload_image'])){ 
            if($_FILES['userImage']['error'] == 0 ){
                $up = move_uploaded_file($_FILES['userImage']['tmp_name'], $image_folder.$_FILES['userImage']['name']);
                if($up){
                    $uploaded = true;   
                }
            }
    }

然后切换回:

chdir($storeOriginalPath);

所以我正在考虑放入chdir($_SERVER['DOCUMENT_ROOT'])在我所有PHP页面的开头,所有内容都与根相关(这就是我在ASP中习惯的),这是常见的、不明智的、聪明的、有气味的还是纯粹愚蠢的?

这:

PHP Warning:  move_uploaded_file(): open_basedir restriction in effect.
File(C:'Windows'Temp'php393F.tmp) is not within the allowed path(s):
(F:'PLESK'WWW'mydomain.com'httpdocs') in
F:'PLESK'WWW'mydomain.com'httpdocs'sparklyphp'cms'modules'articles'edit'photos'index.php on line 40

基本上是说,即使是你的临时文件夹也是不允许的。AFAIK,这显然是一个错误的配置,你应该联系你的主机来修复它。或者,如果你像你说的那样拥有完全的管理员访问权限,只需将open_basedir限制更改为正常的限制。此页面似乎包含有关更改/删除open_basedir设置的说明。

路径错误

$image_folder = "/img/articles/original/";
...
$up = move_uploaded_file($_FILES['userImage']['tmp_name'],  $image_folder...

上面的代码将尝试将文件移动到windows系统上的绝对位置/img/...,我认为这将被解释为例如F:'img'...

默认情况下,Plesk只允许php应用程序写入域的文档根目录或tmp文件夹,因此您可能需要更改目标文件夹路径:

 // edit this and make sure this points somewhere writable
$image_folder = "./img/articles/original/";

要写入文档根目录下的文件夹