99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

CodeIgniter Zip 編碼類

2018-07-21 15:40 更新

Zip 編碼類

CodeIgniter 的 Zip 編碼類允許你創(chuàng)建 Zip 壓縮文檔,文檔可以被下載到你的桌面 或者 保存到某個文件夾里。

使用 Zip 編碼類

初始化類

正如 CodeIgniter 中的其他類一樣,在你的控制器中使用 $this->load->library() 方法來初始化 Zip 編碼類:

$this->load->library('zip');

初始化之后,Zip 編碼類的對象就可以這樣訪問:

$this->zip

使用示例

下面這個例子演示了如何壓縮一個文件,將其保存到服務(wù)器上的一個目錄下,并下載到你的桌面上。

$name = 'mydata1.txt';
$data = 'A Data String!';

$this->zip->add_data($name, $data);

// Write the zip file to a folder on your server. Name it "my_backup.zip"
$this->zip->archive('/path/to/directory/my_backup.zip');

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

類參考

classCI_Zip

$compression_level = 2

使用的壓縮等級。

壓縮等級的范圍為 0 到 9 ,9 為最高等級,0 為禁用壓縮:

$this->zip->compression_level = 0;

add_data($filepath[, $data = NULL])

參數(shù):

  • $filepath (mixed) -- A single file path or an array of file => data pairs
  • $data (array) -- File contents (ignored if $filepath is an array)

返回類型: void

向 Zip 文檔中添加數(shù)據(jù),可以添加單個文件,也可以添加多個文件。

當(dāng)添加單個文件時,第一個參數(shù)為文件名,第二個參數(shù)包含文件的內(nèi)容:

$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);

$name = 'mydata2.txt';
$data = 'Another Data String!';
$this->zip->add_data($name, $data);

當(dāng)添加多個文件時,第一個參數(shù)為包含 file => contents 這樣的鍵值對的數(shù)組,第二個參數(shù)被忽略:

$data = array(
    'mydata1.txt' => 'A Data String!',
    'mydata2.txt' => 'Another Data String!'
);

$this->zip->add_data($data);

如果你想要將你壓縮的數(shù)據(jù)組織到一個子目錄下,只需簡單的將文件路徑包含到文件名中即可:

$name = 'personal/my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);

上面的例子將會把 my_bio.txt 文件放到 personal 目錄下。

add_dir($directory)

參數(shù):

  • $directory (mixed) -- Directory name string or an array of multiple directories

返回類型: void

允許你往壓縮文檔中添加一個目錄,通常這個方法是沒必要的,因為你完全可以使用 $this->zip->add_data() 方法將你的數(shù)據(jù)添加到特定的目錄下。但是如果你想創(chuàng)建一個空目錄,你可以使用它:

$this->zip->add_dir('myfolder'); // Creates a directory called "myfolder"

read_file($path[, $archive_filepath = FALSE])

參數(shù):

  • $path (string) -- Path to file
  • $archive_filepath (mixed) -- New file name/path (string) or (boolean) whether to maintain the original filepath

返回: TRUE on success, FALSE on failure

返回類型: bool

允許你壓縮一個已經(jīng)存在于你的服務(wù)器上的文件。該方法的參數(shù)為一個文件路徑,Zip 類會讀取該文件的內(nèi)容并添加到壓縮文檔中:

$path = '/path/to/photo.jpg';

$this->zip->read_file($path);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

如果你希望 Zip 文檔中的文件保持它原有的目錄結(jié)構(gòu),將第二個參數(shù)設(shè)置為布爾值 TRUE 。例如:

$path = '/path/to/photo.jpg';

$this->zip->read_file($path, TRUE);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

在上面的例子中,photo.jpg 文件將會被放在 path/to/ 目錄下。

你也可以為新添加的文件指定一個新的名稱(包含文件路徑):

$path = '/path/to/photo.jpg';
$new_path = '/new/path/some_photo.jpg';

$this->zip->read_file($path, $new_path);

// Download ZIP archive containing /new/path/some_photo.jpg
$this->zip->download('my_archive.zip');

read_dir($path[, $preserve_filepath = TRUE[, $root_path = NULL]])

參數(shù):

  • $path (string) -- Path to directory
  • $preserve_filepath (bool) -- Whether to maintain the original path
  • $root_path (string) -- Part of the path to exclude from the archive directory

返回: TRUE on success, FALSE on failure

返回類型: bool

允許你壓縮一個已經(jīng)存在于你的服務(wù)器上的目錄(包括里面的內(nèi)容)。該方法的參數(shù)為目錄的路徑,Zip 類會遞歸的讀取它里面的內(nèi)容并重建成一個 Zip 文檔。指定目錄下的所有文件以及子目錄下的文件都會被壓縮。 例如:

$path = '/path/to/your/directory/';

$this->zip->read_dir($path);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

默認(rèn)情況下,Zip 文檔中會保留第一個參數(shù)中指定的目錄結(jié)構(gòu),如果你希望忽略掉這一大串的樹形目錄結(jié)構(gòu), 你可以將第二個參數(shù)設(shè)置為布爾值 FALSE 。例如:

$path = '/path/to/your/directory/';

$this->zip->read_dir($path, FALSE);

上面的代碼將會創(chuàng)建一個 Zip 文檔,文檔里面直接是 "directory" 目錄,然后是它下面的所有的子目錄, 不會包含 /path/to/your 路徑在里面。

archive($filepath)

參數(shù):

  • $filepath (string) -- Path to target zip archive

返回: TRUE on success, FALSE on failure

返回類型: bool

向你的服務(wù)器指定目錄下寫入一個 Zip 編碼文檔,該方法的參數(shù)為一個有效的目錄路徑,后加一個文件名, 確保這個目錄是可寫的(權(quán)限為 755 通常就可以了)。例如:

$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip

download($filename = 'backup.zip')

參數(shù):

  • $filename (string) -- Archive file name

返回類型: void

從你的服務(wù)器上下載 Zip 文檔,你需要指定 Zip 文檔的名稱。例如:

$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"

注解

在調(diào)用這個方法的控制器里不要顯示任何數(shù)據(jù),因為這個方法會發(fā)送多個服務(wù)器 HTTP 頭, 讓文件以二進(jìn)制的格式被下載。

get_zip()

返回: Zip file content
返回類型: string

返回使用 Zip 編碼壓縮后的文件數(shù)據(jù),通常情況你無需使用該方法,除非你要對壓縮后的數(shù)據(jù)做些特別的操作。 例如:

$name = 'my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);

$zip_file = $this->zip->get_zip();

clear_data()

返回類型: void

Zip 類會緩存壓縮后的數(shù)據(jù),這樣就不用在調(diào)用每個方法的時候重新壓縮一遍了。但是,如果你需要創(chuàng)建多個 Zip 文檔,每個 Zip 文檔有著不同的數(shù)據(jù),那么你可以在多次調(diào)用之間把緩存清除掉。例如:

$name = 'my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);
$zip_file = $this->zip->get_zip();

$this->zip->clear_data();

$name = 'photo.jpg';
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents

$this->zip->download('myphotos.zip');
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號