Home > Backend Development > PHP Tutorial > PHP用mkdir方法创建文件夹报错

PHP用mkdir方法创建文件夹报错

WBOY
Release: 2016-06-06 20:52:07
Original
1289 people have browsed it

我想在上传图片时根据当前日期按年月日来放图片,于是就有了如下代码

<?php $now_date = date("Y/m/d") . '/';
$path = dirname(__FILE__);
$directory = $path.'/image/'.$now_date;
print $directory;
!is_dir($directory) && mkdir($directory,0777);
?>
Copy after login
Copy after login

以上代码在本地测试环境(PHP+apache+MySQL)测试通过没有问题。但当我上传代码到服务器的时候会报如下错误
Warning: mkdir() [function.mkdir]: open_basedir restriction in effect. File(D:\hosting) is not within the allowed path(s): (D:\hosting\wwwroot\;D:\hosting\System\;C:\WINDOWS\Temp\) in D:\hosting\wwwroot\obbzz_com\htdocs\mytest.php on line 47

请问这种情况一般是由什么原因导致的?

回复内容:

我想在上传图片时根据当前日期按年月日来放图片,于是就有了如下代码

<?php $now_date = date("Y/m/d") . '/';
$path = dirname(__FILE__);
$directory = $path.'/image/'.$now_date;
print $directory;
!is_dir($directory) && mkdir($directory,0777);
?>
Copy after login
Copy after login

以上代码在本地测试环境(PHP+apache+MySQL)测试通过没有问题。但当我上传代码到服务器的时候会报如下错误
Warning: mkdir() [function.mkdir]: open_basedir restriction in effect. File(D:\hosting) is not within the allowed path(s): (D:\hosting\wwwroot\;D:\hosting\System\;C:\WINDOWS\Temp\) in D:\hosting\wwwroot\obbzz_com\htdocs\mytest.php on line 47

请问这种情况一般是由什么原因导致的?

你生成多级目录的地方有问题,php不可能一次生成多级目录date("Y/m/d"),你必须循环检测目录是否存在,如果不存在然后再一级一级的创建

看来明显是没有翻阅过 PHP 官方文档

http://php.net/manual/en/function.mkd...

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

第三个参数为 true 就可以了,类似于 Java 的 File.mkdirs

下面这个是我使用的函数,挺好用的。

function mkdirs($dir)  
    {  
        if(!is_dir($dir))  
        {  
            if(!mkdirs(dirname($dir))){  
                return false;
            }  
            if(!mkdir($dir,0777)){  
                return false;
            }
            chmod($dir, 0777); 
        }  
        return true;  
    }
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template