2012-07-25 49 views
0

我很确定我根本没有权限。无论如何,谢谢你的回答!我将切换到自我托管,所以我知道我有权限!PHP - 制作文件夹中的文件夹

(我会删除这一点,但它说我不能B/C有答案)

+3

也许文件夹已经存在,你想先检查一下吗? – Ibu 2012-07-25 19:47:17

+0

它不!我试着用差异用户名,每次它不存在 – Primm 2012-07-25 19:47:34

+0

看起来你应该添加一个检查周围的mkdir如果目录存在,或者可能抑制错误/警告 – ernie 2012-07-25 19:47:37

回答

0

这是为这种情况的算法。

  1. 检查文件夹是否存在。
  2. 如果文件夹存在,则将该文件夹命名为其他名称或向其添加一个随机数。
  3. 创建新文件夹。

    http://pastebin.com/VefbrzRS

0

试试这个。

mkdir ("./files/$username"); 
0

不是那些相同的文件夹?文件/ Alex和文件/ Alex /是相同的。你的意思是文件/ $用户名和文件/ $用户名/文件?你正在做同样的目录两次,所以这是错误

+0

这些是我已经尝试过的两种不同的东西 – Primm 2012-07-25 20:00:48

+0

转到命令行并执行mkdir文件/测试然后在不更改目录的情况下执行mkdir文件/ test /您将得到相同的错误 – 2012-07-25 20:02:32

0

如果你在Linux或MacOs上,还有另一种情况,将调用你的shell的mkdir函数。

它会看起来像:

system('mkdir -p yourdir/files/$username') 
1

首先,什么是$username的实际价值?你是否证实它不是空的?

像这样处理文件系统会导致几个不同的问题。我喜欢进行很多额外的检查,所以如果出现问题,我会更容易知道原因。我也喜欢在可能的情况下处理绝对目录名称,所以我不会遇到相对路径问题。

$filesDir = '/path/to/files'; 
if (!file_exists($filesDir) || !is_dir($filesDir)) { 
    throw new Exception("Files directory $filesDir does not exist or is not a directory"); 

} else if (!is_writable($filesDir)) { 
    throw new Exception("Files directory $filesDir is not writable"); 
} 

if (empty($username)) { 
    throw new Exception("Username is empty!"); 
} 

$userDir = $filesDir . '/' . $username; 

if (file_exists($userDir)) { 
    // $userDir should be all ready to go; nothing to do. 
    // You could put in checks here to make sure it's 
    // really a directory and it's writable, though. 

} else if (!mkdir($userDir)) { 
    throw new Exception("Creating user dir $userDir failed for unknown reasons."); 
} 

mkdir()有一些非常有用的选项,用于设置权限并使文件夹层次更深。如果你还没有,请查看PHP's mkdir page

为了安全起见,请确保您的例外情况不会向最终用户透露系统路径。当您的代码进入公共服务器时,您可能希望从错误消息中删除文件夹路径。或者配置一些东西,以便您的例外被记录但不会显示在网页上。

相关问题