sfGetDirSize($path), FILE_NAME_LEN); $file_time = date("Y/m/d", filemtime($path)); // ディレクトリとファイルで格納配列を変える if(is_dir($path)) { $arrDirList[$cnt]['file_name'] = $file; $arrDirList[$cnt]['file_path'] = $path; $arrDirList[$cnt]['file_size'] = $file_size; $arrDirList[$cnt]['file_time'] = $file_time; $arrDirList[$cnt]['is_dir'] = true; } else { $arrFileList[$cnt]['file_name'] = $file; $arrFileList[$cnt]['file_path'] = $path; $arrFileList[$cnt]['file_size'] = $file_size; $arrFileList[$cnt]['file_time'] = $file_time; $arrFileList[$cnt]['is_dir'] = false; } $cnt++; } } closedir($dh); } } // フォルダを先頭にしてマージ return array_merge($arrDirList, $arrFileList); } /** * 指定したディレクトリのバイト数を取得する. * * @param string $dir ディレクトリ * @return void */ function sfGetDirSize($dir) { $bytes = 0; if(file_exists($dir)) { // ディレクトリの場合下層ファイルの総量を取得 if (is_dir($dir)) { $handle = opendir($dir); while ($file = readdir($handle)) { // 行末の/を取り除く $dir = ereg_replace("/$", "", $dir); $path = $dir."/".$file; if ($file != '..' && $file != '.' && !is_dir($path)) { $bytes += filesize($path); } else if (is_dir($path) && $file != '..' && $file != '.') { // 下層ファイルのバイト数を取得する為、再帰的に呼び出す。 $bytes += $this->sfGetDirSize($path); } } } else { // ファイルの場合 $bytes = filesize($dir); } } // ディレクトリ(ファイル)が存在しない場合は0byteを返す if($bytes == "") $bytes = 0; return $bytes; } /** * 指定したディレクトリ又はファイルを削除する. * * @param string $dir 削除するディレクトリ又はファイル * @return void */ function sfDeleteDir($dir) { $arrResult = array(); if(file_exists($dir)) { // ディレクトリかチェック if (is_dir($dir)) { if ($handle = opendir("$dir")) { $cnt = 0; while (false !== ($item = readdir($handle))) { if ($item != "." && $item != "..") { if (is_dir("$dir/$item")) { $this->sfDeleteDir("$dir/$item"); } else { $arrResult[$cnt]['result'] = @unlink("$dir/$item"); $arrResult[$cnt]['file_name'] = "$dir/$item"; } } $cnt++; } } closedir($handle); $arrResult[$cnt]['result'] = @rmdir($dir); $arrResult[$cnt]['file_name'] = "$dir/$item"; } else { // ファイル削除 $arrResult[0]['result'] = @unlink("$dir"); $arrResult[0]['file_name'] = "$dir"; } } return $arrResult; } /** * ツリー生成用配列取得(javascriptに渡す用). * * @param string $dir ディレクトリ * @param string $tree_status 現在のツリーの状態開いているフォルダのパスを * | 区切りで格納 * @return array ツリー生成用の配列 */ function sfGetFileTree($dir, $tree_status) { $cnt = 0; $arrTree = array(); $default_rank = count(split('/', $dir)); // 文末の/を取り除く $dir = ereg_replace("/$", "", $dir); // 最上位層を格納(user_data/) if($this->sfDirChildExists($dir)) { $arrTree[$cnt]['type'] = "_parent"; } else { $arrTree[$cnt]['type'] = "_child"; } $arrTree[$cnt]['path'] = $dir; $arrTree[$cnt]['rank'] = 0; $arrTree[$cnt]['count'] = $cnt; // 初期表示はオープン if($_POST['mode'] != '') { $arrTree[$cnt]['open'] = $this->lfIsFileOpen($dir, $tree_status); } else { $arrTree[$cnt]['open'] = true; } $cnt++; $this->sfGetFileTreeSub($dir, $default_rank, $cnt, $arrTree, $tree_status); return $arrTree; } /** * ツリー生成用配列取得(javascriptに渡す用). * * @param string $dir ディレクトリ * @param string $default_rank デフォルトの階層 * (/区切りで 0,1,2・・・とカウント) * @param integer $cnt 連番 * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが * | 区切りで格納 * @return array ツリー生成用の配列 */ function sfGetFileTreeSub($dir, $default_rank, &$cnt, &$arrTree, $tree_status) { if(file_exists($dir)) { if ($handle = opendir("$dir")) { while (false !== ($item = readdir($handle))) $arrDir[] = $item; // アルファベットと数字でソート natcasesort($arrDir); foreach($arrDir as $item) { if ($item != "." && $item != "..") { // 文末の/を取り除く $dir = ereg_replace("/$", "", $dir); $path = $dir."/".$item; // ディレクトリのみ取得 if (is_dir($path)) { $arrTree[$cnt]['path'] = $path; if($this->sfDirChildExists($path)) { $arrTree[$cnt]['type'] = "_parent"; } else { $arrTree[$cnt]['type'] = "_child"; } // 階層を割り出す $arrCnt = split('/', $path); $rank = count($arrCnt); $arrTree[$cnt]['rank'] = $rank - $default_rank + 1; $arrTree[$cnt]['count'] = $cnt; // フォルダが開いているか $arrTree[$cnt]['open'] = $this->lfIsFileOpen($path, $tree_status); $cnt++; // 下層ディレクトリ取得の為、再帰的に呼び出す $this->sfGetFileTreeSub($path, $default_rank, $cnt, $arrTree, $tree_status); } } } } closedir($handle); } } /** * 指定したディレクトリ配下にファイルがあるかチェックする. * * @param string ディレクトリ * @return bool ファイルが存在する場合 true */ function sfDirChildExists($dir) { if(file_exists($dir)) { if (is_dir($dir)) { $handle = opendir($dir); while ($file = readdir($handle)) { // 行末の/を取り除く $dir = ereg_replace("/$", "", $dir); $path = $dir."/".$file; if ($file != '..' && $file != '.' && is_dir($path)) { return true; } } } } return false; } /** * 指定したファイルが前回開かれた状態にあったかチェックする. * * @param string $dir ディレクトリ * @param string $tree_status 現在のツリーの状態開いているフォルダのパスが * | 区切りで格納 * @return bool 前回開かれた状態の場合 true */ function lfIsFileOpen($dir, $tree_status) { $arrTreeStatus = split('\|', $tree_status); if(in_array($dir, $arrTreeStatus)) { return true; } return false; } /** * ファイルのダウンロードを行う. * * @param string $file ファイルパス * @return void */ function sfDownloadFile($file) { // ファイルの場合はダウンロードさせる Header("Content-disposition: attachment; filename=".basename($file)); Header("Content-type: application/octet-stream; name=".basename($file)); Header("Cache-Control: "); Header("Pragma: "); echo ($this->sfReadFile($file)); } /** * ファイル作成を行う. * * @param string $file ファイルパス * @param integer $mode パーミッション * @return bool ファイル作成に成功した場合 true */ function sfCreateFile($file, $mode = "") { // 行末の/を取り除く if($mode != "") { $ret = @mkdir($file, $mode); } else { $ret = @mkdir($file); } return $ret; } /** * ファイル読込を行う. * * @param string ファイルパス * @return string ファイルの内容 */ function sfReadFile($filename) { $str = ""; // バイナリモードでオープン $fp = @fopen($filename, "rb" ); //ファイル内容を全て変数に読み込む if($fp) { $str = @fread($fp, filesize($filename)+1); } @fclose($fp); return $str; } /** * ファイル書込を行う. * * @param string $filename ファイルパス * @param string $value 書き込み内容 */ function sfWriteFile($filename, $value) { $fp = @fopen($filename, "w"); // ファイルに書き込む if($fp) { fwrite($fp, $value); } @fclose($fp); } /** * ユーザが作成したファイルをアーカイブしダウンロードさせる * TODO 要リファクタリング * @param void * @return void */ function downloadArchiveFiles($dir) { $debug_message = ""; // ダウンロードされるファイル名 $dlFileName = 'tpl_package_' . date('YmdHis') . '.tar.gz'; // ファイル一覧取得 $arrFileHash = SC_Utils::sfGetFileList($dir); foreach($arrFileHash as $val) { $arrFileList[] = $val['file_name']; $debug_message.= "圧縮:".$val['file_name']."\n"; } GC_Utils::gfDebugLog($debug_message); // ディレクトリを移動 chdir($dir); // 圧縮をおこなう $tar = new Archive_Tar($dlFileName, true); $tar->create($arrFileList); // ダウンロード用HTTPヘッダ出力 header("Content-disposition: attachment; filename=${dlFileName}"); header("Content-type: application/octet-stream; name=${dlFileName}"); header("Content-Length: " . filesize($dlFileName)); readfile($dlFileName); unlink($dir . "/" . $dlFileName); exit; } /** * tarアーカイブを解凍する. * * @param string $path アーカイブパス * @return string Archive_Tar::extractModify()のエラー */ function unpackFile($path) { // 圧縮フラグTRUEはgzip解凍をおこなう $tar = new Archive_Tar($path, true); $dir = dirname($path); $file_name = basename($path); // 拡張子を切り取る $unpacking_name = preg_replace("/(\.tar|\.tar\.gz)$/", "", $file_name); // 指定されたフォルダ内に解凍する $tar->extractModify($dir. "/", $unpacking_name); GC_Utils_Ex::gfPrintLog("解凍:" . $dir."/".$file_name."->".$dir."/".$unpacking_name); // フォルダ削除 SC_Utils::sfDelFile($dir . "/" . $unpacking_name); // 圧縮ファイル削除 unlink($path); } } ?>