2011-06-19 44 views
2

我想创建一个名为upload的文件夹内的目录。这是我目前使用的简单代码,我试图完成这项工作:如何通过PHP创建子文件夹?

<?php 

$thisdir = getcwd(); 
$new_dir = '145'; 


if(mkdir("/upload/" . $newdir, 0777)) 
{ 
echo "Directory has been created successfully..."; 
} 
else 
{ 
echo "Failed to create directory..."; 
} 
?> 

必须有一些简单的修复程序,我错过了。你可以放置它吗?谢谢。

+0

是否有php(apache,无论...)有权限在那里创建一个目录? –

回答

2

默认情况下,您的php守护进程将无权写入名为/upload(/为根目录)的文件夹。您需要将此文件夹写入该命令才能工作。

但是,这样说,这个问题肯定是由于您的代码出错导致的,并且您不在当前目录中工作。我猜你的代码应该阅读...

if(mkdir($thisdir . "/upload/" . $newdir, 0777)) 

在这种情况下,它应该可以正常工作。

+0

甜!那样做了。谢谢 :) – Presto

3

/upload/几乎肯定是错的。它指向根目录目录中的“上传”目录,在那里你很可能没有权利创建目录。

你可能是指upload/

0
//new folder you want to create 
$newfolder="145"; 
//get the current working directory. 
$curdir= getcwd(); 
//append the "upload" folder which already exits in ur working directory alog with the new directory name "$newfolder" 
$dir= $curdir."\uploads"."/$newfolder"; 
//check if directory exits 
if(is_dir($dir)) 
{ 
    echo " Directory exists"; 
} 
else 
{ 
    //create new directory recursively 
    mkdir($dir,0777,true); 
    echo " Directory Created"; 
} 
相关问题