2014-09-22 58 views
1

这是我想让它能够通过用户名为用户创建多个目录的代码。如何通过用户名为用户创建多个目录

现在它由big

<?php 
    $db = new PDO("..."); // Connection details here 
    $stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work 
    $stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement 
    $row = $stmt->fetch(); 

    // Then run your code 

    if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) { 
     mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true); 
    } 
?> 

名称创建我想其他两个目录来这样一个文件夹:

user/upload/'.$row['UserName'].'/avatar/small 

user/upload/'.$row['UserName'].'/avatar/original 
+3

只是不。老实说:不。真的......虽然这是可能的,但你真的不应该想到梦想着走这条道路。有龙... – 2014-09-22 09:53:25

回答

0

所有你需要做的是改变我给你以下的原始脚本:

<?php 
    $db = new PDO("..."); // Connection details here 
    $stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work 
    $stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement 
    $row = $stmt->fetch(); 

    // Then run your code 

    if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) { 
     mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true); 
    } 

    if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small')) { 
     mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small', 0777, true); 
    } 

    if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original')) { 
     mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original', 0777, true); 
    } 
?> 
+0

真的很感谢。我可以使用UserName而不是user_id – arafi 2014-09-22 11:41:29

+0

您可以,您可以将选择语句更改为适合您的任何内容。上面的逻辑只是我个人用来使事情工作:) – 2014-09-22 11:51:09

+0

如果我改变它使用UserName而不是'user_id'那么我应该把什么,而不是':ID' – arafi 2014-09-22 11:52:42

3

你已经有解决方案在这里不你。继续做你已经做什么..

if(!is_dir((ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')){ 
    mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true); 
} 

if(!is_dir ((ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')){ 
    mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true); 
} 

1

您需要创建用户名的一个文件夹,比创建用户名文件夹下的文件夹等根据自己的需要和那里的访问会类似。

if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big')) { 
    @mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/big', 0777, true); 
} 
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small')) { 
    @mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/small', 0777, true); 
} 
if (!file_exists(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original')) { 
    @mkdir(ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/original', 0777, true); 
} 
+0

它应该如何检查文件夹是否存在 – arafi 2014-09-22 09:59:07

+2

并避免不惜一切代价使用@,因为它相当于:禁用错误处理程序,调用mkdir,启用错误处理程序等,对性能非常不利 – kitensei 2014-09-22 10:02:45

2

通过你的结果只是循环,并创建目录(is_dir比file_exists更快)

$db = new PDO("..."); // Connection details here 
$stmt = $db->prepare("SELECT * from some_table where user_id = :id"); // Finds the information based on an ID. Change this depending on how you want the select to work 
$stmt->execute(array(':id' => "1")); // Gives value to :id and executes the statement 

// Then run your code 
while ($row = $stmt->fetch()) { 

    array_walk(array('big', 'small', 'original'), function(&$v, $k) { 
     $dir = ROOT_PATH.'user/upload/'.$row['UserName'].'/avatar/' . $v; 
     if (!is_dir($dir)) { 
      mkdir($dir, 0777, true); 
     } 
    } 
} 
+0

谢谢但它给了我错误,请参阅jsfiddle链接以获得更多解释http://jsfiddle.net/g6antdqy/ – arafi 2014-09-22 10:29:38

+0

你会得到什么错误? – kitensei 2014-09-22 10:35:11

+0

这些是错误http://jsfiddle.net/d9ve5yd7/ – arafi 2014-09-22 10:45:50