* @author yuji TAKAHASHI */ /* fileDownload ファイルをダウンロード(ブラウザ表示)する。ダウンロード時のファイル名を設定可。 引数: $filePath ダウンロードするファイルのフルパス $filename ダウンロード時のファイル名 戻り値:正常終了 True 以上終了 False */ function fileDownload($filePath, $filename = "") { if ($filename == "") { $filename = basename($filePath); } if (mb_detect_encoding($filename, "auto") != "SJIS") { $filename = mb_convert_encoding($filename, "SJIS", "auto"); } mb_http_output("pass"); if (stristr($filename, ".tar.gz") != FALSE) { $applicationStr = "x-tar"; } elseif (stristr($filename, ".lzh") != FALSE) { $applicationStr = "x-lzh"; } elseif (stristr($filename, ".pdf") != FALSE) { $applicationStr = "pdf"; } elseif (stristr($filename, ".zip") != FALSE) { $applicationStr = "zip"; } elseif (stristr($filename, ".cvs") != FALSE) { $applicationStr = "x-cvs"; } elseif (stristr($filename, ".xls") != FALSE) { $applicationStr = "vnd.ms-excel"; } elseif (stristr($filename, ".doc") != FALSE) { $applicationStr = "vnd.ms-word"; } elseif (stristr($filename, ".ppt") != FALSE) { $applicationStr = "vnd.ms-powerpoint"; } else { $applicationStr = "octet-stream"; } header("Content-Type: application/" . $applicationStr); header("Content-Disposition: inline; filename=\"" . $filename . "\""); header("Content-Length: " . filesize($filePath)); readfile($filePath); return True; } /* fileDelete ファイルを削除する。 引数: $filePath 削除するファイルのフルパス 戻り値:なし */ function fileDelete($filePath) { unlink($filePath); } /* fileMove アップロードされたファイルを移動(退避)する。 移動先のディレクトリが存在しない場合、作成する。 引数: $tempfilePath テンポラリファイルパス $filePath 移動先のファイルパス 戻り値:なし */ function fileMove($tempfilePath, $filePath) { makeDirectory(dirname($filePath)); move_uploaded_file($tempfilePath, $filePath); } /* makeDirectory ディレクトリを作成する。ルートの途中で存在しないディレクトリがあった場合、その都度作成する。 引数: $dirPath 作成するディレクトリのパス 戻り値:なし */ function makeDirectory($dirPath) { if (trim($dirPath) == "") { return False; } $dirname = strtok($dirPath, "/"); for ($i = 0; $dirname; $i++) { $dirnameArr[$i] = $dirname; $dirname = strtok("/"); } // $dirnameTemp = "/"; for ($i = 0; $i < count($dirnameArr); $i++) { if (!file_exists($dirnameTemp . $dirnameArr[$i])) { mkdir($dirnameTemp . $dirnameArr[$i], 0755); } $dirnameTemp .= $dirnameArr[$i] . "/"; } } /* createFile ファイルを作成する。 引数 $dirPath 作成するファイルのパス $fileName 作成するファイル名 $value ファイルの中身 戻り値 なし */ function createFile($dirPath, $fileName, $value) { $fileName = trim($fileName); $dirPath = trim($dirPath); if (!$fileName || !$dirPath || !$value) { return False; } $filePath = $dirPath . "/" . $fileName; makeDirectory(dirname($filePath)); touch($filePath); //file作成 $fp = fopen($filePath, "w"); //ファイルオープン if (!$fp) { return False; } if (mb_detect_encoding($value, "auto") != "SJIS") { $value = mb_convert_encoding($value, "SJIS", "auto"); } fwrite ($fp, $value); fclose($fp); //ファイルクローズ } ?>