[PHP-users 33373] Re: PHPでサーバのディレクトリを削除

t-bird t-bird @ connectworks.jp
2008年 3月 8日 (土) 17:40:39 JST


岡本さん、TKDさん、ありがとうございます。

最初に私がどういう前提で何をしたいかということがはっきり
お伝えしなかったので、誤解を招いたかも知れません。

結局、システム側で用意したディレクトリにユーザーがFTPによって
アップしたファイルを、ディレクトリごとシステム側で消すって事
なので、やはり最初の思惑通りFTP関数を使うしかないのかと思い直
し、いろいろネットを徘徊していましたらマニュアルにそのもの
ずばりが載っていました。

皆さんにとっては参考にならないかも知れませんが、念のために
引用しておきます。

romainさんのものです。

function DeleteDirRecursive($resource, $path) {
    $result_message = "";
    $list = ftp_nlist ($resource, $path);
    if ( empty($list) ) {
        $list = RawlistToNlist( ftp_rawlist($resource, $path), $path . ( substr($path, strlen($path) - 1, 1) == "/" ? "" : "/" ) );
    }
    if ($list[0] != $path) {
        $path .= ( substr($path, strlen($path)-1, 1) == "/" ? "" : "/" );
        foreach ($list as $item) {
        if ($item != $path.".." && $item != $path.".") {
            $result_message .= DeleteDirRecursive($resource, $item);
        }
        }
        if (ftp_rmdir ($resource, $path)) {
            $result_message .= "Successfully deleted $path <br />\n";
        } else {
            $result_message .= "There was a problem while deleting $path <br />\n";
        }
    }
    else {
        if (ftp_delete ($resource, $path)) {
            $result_message .= "Successfully deleted $path <br />\n";
        } else {
            $result_message .= "There was a problem while deleting $path <br />\n";
        }
    }
    return $result_message;
}

function RawlistToNlist($rawlist, $path) {
    $array = array();
    foreach ($rawlist as $item) {
        $filename = trim(substr($item, 55, strlen($item) - 55));
        if ($filename != "." || $filename != "..") {
        $array[] = $path . $filename;
        }
    }
    return $array;
}

これを少し加工し、思い通りのことができました。

いろいろアドバイスを下さった方ありがとうございました。
大変勉強になりました。

--
サトウ


PHP-users メーリングリストの案内