如何在php中分解文件夹路径

时间:2011-01-20 10:10:22

标签: php

我正在尝试分解文件夹路径

例如:home/player/jay/profile/pictures

home
home/players
home/players/jay
home/players/jay/profile
home/players/jay/profile/pictures

我尝试使用这个,但我不能让它显示正确的

$new_folders = explode("/","home/player/jay/profile/pictures");

for ($i = 0; $i < sizeof($new_folders); $i++) {
    for ($r = 0; $r < $i + 1; $r++) {
        $create_new_path .=  $new_folders[$r];
        if ($r != $i) {
            $create_new_path .= "/";
        }
    }

    //ftp_mkdir($conn_id, "httpdocs/user_images/".$create_new_path);
    //ftp_chmod($conn_id, 0777, "httpdocs/user_images/".$create_new_path);
}

1 个答案:

答案 0 :(得分:5)

$breakdown = array();
$dirs = explode('/', 'home/player/jay/profile/pictures');
$path = '';
foreach($dirs as $dir) {
    $path .= ($path !== '' ? '/' : '') . $dir;
    $breakdown[] = $path;
}
// result is in $breakdown