Pear のインストールは Windows 環境で Pear のインストール を参照して下さい。インストールは Windows ベースですが、Pear ディレクトリ内のライブラリをレンタルサーバーにアップロードして動作させる事ができます。 ※ zip 圧縮には、File_Archive が必要です このサンプルでは、相対参照によるファイルのパス一覧を配列にセットする事によって全てを zip 圧縮してダウンロードさせます $files[] = "./" の指定で、中にあるサブフォルダも対象となります。 圧縮という仕様上、ファイルサイズを content-length で送る事ができないので、ダウンロード時にプログレスバーが出る事はありません。どうしても必要な場合は、zip ファイルを一時名でサーバーで作成して、そのファイルを読み込んでダウンロードすれば良いと思いますが、効率が良いとはけっして言えないので小さなファイルならばこれで十分だと思います
<?php // ********************************************* // PHP 5.3 以降で必要 // ********************************************* error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE ); // ********************************************* // Pear ライブリを使用する為に、include パスを設定し、 // 必要なライブラリを読み込む // ********************************************* set_include_path( "c:/php5217/PEAR" ); require_once "File/Archive.php"; // ********************************************* // 圧縮レベル // "zipCompressionLevel" // Value between 0 and 9 specifying the default // compression level used by Zip writers // (0 no compression, 9 highest compression) // Default: 9 // ********************************************* File_Archive::setOption('zipCompressionLevel', 9); // ダウンロード用アーカイブを決定 $zip = File_Archive::toArchive( "zip_sample.zip", File_Archive::toOutput() ); // このディレクトリそのものを追加する $files[] = "./"; // 別のディレクトリのファイルを追加 $files[] = "../../twitter/callback.php"; File_Archive::extract( $files, $zip ); ?>
※ 単独ファイルの場合は、二つ以上の上の階層まで読みだせるのですが、 ※ ディレクトリの場合は、一つ上の階層までしか読めませんでした。 ※ ディレクトリが読める場合は、"*.php" のようなワイルドカードを指定できます ▼ 以下は、setOption のオプションの一覧です
"cache" Instance of a Cache_Lite object used to cache some compressed data to speed up future compressions of files Default: null (no cache used) "zipCompressionLevel" Value between 0 and 9 specifying the default compression level used by Zip writers (0 no compression, 9 highest compression) Default: 9 "gzCompressionLevel" Value between 0 and 9 specifying the default compression level used by Gz writers (0 no compression, 9 highest compression) Default: 9 "tmpDirectory" Directory where the temporary files generated by File_Archive will be created Default: '.' "appendRemoveDuplicates" If set to true, the appender created will by default remove the file present in the archive when adding a new one. This will slow the appending of files to archives Default: false "blockSize" To transfer data from a reader to a writer, some chunks a read from the source and written to the writer. This parameter controls the size of the chunks Default: 64kB "cacheCondition" This parameter specifies when a cache should be used. When the cache is used, the data of the reader is saved in a temporary file for future access.The cached reader will be read only once, even if you read it several times.This can be usefull to read compressed files or downloaded files (from http or ftp) The possible values for this option are - false: never use cache - a regexp: A cache will be used if the specified URL matches the regexp preg_match is used Default: false Example: '/^(http|ftp):\/\//' will cache all files downloaded via http or ftp
以下は、動的な単独ファイルの追加と、配列の変わりに File_Archive::readMultiを使用しています
<?php // ********************************************* // PHP 5.3 以降で必要 // ********************************************* error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE ); // ********************************************* // Pear ライブリを使用する為に、include パスを設定し、 // 必要なライブラリを読み込む // ********************************************* set_include_path( "c:/php5217/PEAR" ); require_once "File/Archive.php"; // ********************************************* // 圧縮レベル // "zipCompressionLevel" // Value between 0 and 9 specifying the default // compression level used by Zip writers // (0 no compression, 9 highest compression) // Default: 9 // ********************************************* File_Archive::setOption('zipCompressionLevel', 9); // ダウンロード用アーカイブを決定 $zip = File_Archive::toArchive( "zip_sample.zip", File_Archive::toOutput() ); // 動的な追加 $zip->newFile("mydata/readme.txt"); $zip->writeData( "My name is lightbox." ); $zip->close(); File_Archive::extract( File_Archive::readMulti( array( // このディレクトリそのものを追加する File_Archive::read('./'), // 別のディレクトリのファイルを追加 File_Archive::read('../../twitter_api/GetAccessToken/phase_1.php'), // 一つ上のディレクトリの内容を追加 File_Archive::read('../twitter/*.php' ) ) ), $zip ); ?>
|
【PHPの最新記事】
- ロリポップのモジュール版 PHP でエラーを出力する方法
- PHP : unset によるオブジェクトのプロパティのと配列の要素の削除
- レンタルサーバで PHP のバージョンを 5.4 から 5.6 に変更する時の注意事項
- 【3大手法】 PHP で変数を埋め込んだ画面定義を外部ファイルにして、ループ内で展開する
- PHP で pathinfo と mime 情報やその他を合体した、get_finfo 関数を使って、opendir からファイル一覧を出力
- Windows で、MySQL を使っているので phpMyAdmin で参照しようとして、AN HTTPD だと setup が動かなかったので Apache を入れようとしたらいろいろルールがあっ..
- PHP : ファイルのアップロード時のデータのダンプ( PHP 5.4.0 以降で利用可能な php://input での取得 / ASP / C# )
- PHP で問い合わせ画面のページリンクを常に5つ作成しておいて、jQuery でいらないリンクを非表示にする
- PHP : HTMLのinput要素のname属性に二次元配列を指定して getElementsByName と jQueryのinput[name='value']:eq(n) でアクセステスト
- PHP の簡易ログとしては file_put_contents が使われますが、表現方法をいろいろテストして行くと、PHP のバージョンを 5.4.0 以上にして json_encode を使いたくな..
- さくらのサーバーで hash_hmac のアルゴリズムで使えるもの
- Windows 環境で Pear のインストール
- PHP : php-5.3.1 の日付関数の注意点
- PHP : 暗号化と複合( libmcrypt )
- begin で始まるテキストで添付されたファイル(uuencode)を元に戻す方法