首先介绍一下mkdir()函数:
mkdir(path,mode,recursive,context);
参数 | 描述 |
---|---|
path | 必需。规定要创建的目录的名称。 |
mode | 必需。规定权限。默认是 0777。 |
recursive | 必需。规定是否设置递归模式。 |
context | 必需。规定文件句柄的环境。Context 是可修改流的行为的一套选项。 |
使用iconv或mb_convert_encoding函数转换字符的编码。
//将字符串 str 从 in_charset 转换编码到 out_charset。
string iconv ( string $in_charset , string $out_charset , string $str )
//将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )
代码实例
<?php
header("Content-type:text/html;charset=utf-8");
//要创建的多级目录
$path="dai/php/php学习";
//判断目录存在否,存在给出提示,不存在则创建目录
if (is_dir($path)){
echo "对不起!目录 " . $path . " 已经存在!";
}else{
//第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码
$res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);
if ($res){
echo "目录 $path 创建成功";
}else{
echo "目录 $path 创建失败";
}
}
?>