2012-01-29 130 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); 
}