1

我正在用 PHP 开发一个 CMS 作为学习练习,但遇到了一个叫做“open_basedir 限制”的砖墙——我正在尝试上传一个小的 JPG 文件。我试图尽可能简洁地提供尽可能多的信息,但如果我忘记了什么,请告诉我!

我可以看到它每次都命中 c:/windows/temp/ 文件夹,因此它只会在尝试执行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) 不在允许的路径中: (F:\PLESK\WWW\mydomain.com\httpdocs) 在 F:\PLESK\WWW\mydomain.com\httpdocs \sparklyphp\cms\modules\articles\edit\photos\index.php 在第 40 行

更多错误

如果我改变我的道路

$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

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

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

任何帮助将非常感激!

鲍勃

4

3 回答 3

1

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

$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 中习惯的),这是常见的、不明智的、聪明的、臭的还是只是愚蠢的?

于 2013-08-23T14:26:28.017 回答
1

This:

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

is basically saying even your temp folder is not allowed. AFAIK that would be clearly a misconfiguration and you should contact your hosting to fix it. Or, if you have full admin access like you say you have, just change the open_basedir restriction to something sane. This page looks to contain instrcutions on changing/removing open_basedir settings.

于 2013-08-23T16:29:18.627 回答
0

路径不对

$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/";

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

于 2013-08-23T14:26:49.633 回答