2012-01-29 58 views
0

我一直試圖創建一個特定結構的目錄,但似乎沒有任何事情發生。我已經通過定義如下多個變量走近這個:基於一堆變量在PHP中創建一個目錄

$rid = '/appicons/'; 
$sid = '$artistid'; 
$ssid = '$appid'; 
$s = '/'; 

和功能,我使用了運行正是如此:

$directory = $appid; 
if (!is_dir ($directory)) 
    { 
    mkdir($directory); 
    } 

工程。不過,我想有以下結構中創建目錄:/appicons/$ artistid/$的appid/

但沒有什麼似乎工作。我明白,如果我要添加更多的變量到$目錄,那麼我不得不圍繞它們使用引號並將它們連接起來(這會讓人感到困惑)。

有沒有人有任何解決方案?

回答

3
$directory = "/appicons/$artistid/$appid/"; 
if (!is_dir ($directory)) 
{ 
    //file mode 
    $mode = 0777; 
    //the third parameter set to true allows the creation of 
    //nested directories specified in the pathname. 
    mkdir($directory, $mode, true); 
} 
+1

感謝這個!不過,我必須刪除$ directory變量中的第一個斜槓。 :-) – 2012-01-29 14:42:44

0

這應該做你想要什麼:

$rid = '/appicons/'; 
$sid = $artistid; 
$ssid = $appid; 
$s = '/'; 

$directory = $rid . $artistid . '/' . $appid . $s; 

if (!is_dir ($directory)) { 
    mkdir($directory); 
} 

的原因,您的當前的代碼不工作是因爲你試圖使用字符串字面內部變量的事實。 PHP中的字符串文字是用單引號括起來的字符串(')。這個字符串中的每個字符都被視爲一個字符,因此任何變量都將被解析爲文本。 Unquoting變量讓你的聲明如下所示修復您的問題:

$rid = '/appicons/'; 
$sid = $artistid; 
$ssid = $appid; 
$s = '/'; 

這下一行連接(合併)的變量一起進入的路徑:

$directory = $rid . $artistid . '/' . $appid . $s; 
0

串聯非常喜歡這個

$directory = $rid.$artistid."/".$appid."/" 
0

當您將一個變量分配給另一個變量時,不需要引號,所以以下應該是你在找什麼。

$rid = 'appicons'; 
$sid = $artistid; 
$ssid = $appid; 

然後......

$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/'; 
if (!is_dir($dir)) { 
    mkdir($dir); 
}