2014-09-22 62 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