php读取zip文件,php如何读取zip内容?(zip_entry_read函数的使用)
本篇文章主要給大家介紹PHP如何從打開的 zip 檔案中獲取內容,那么在PHP中有一個內置函數可以實現,即zip_entry_read()函數。
zip_entry_read()函數是PHP中內置的函數,用于從打開的zip歸檔條目中讀取內容。正在讀取zip條目,返回的字節數可以作為參數發送給zip_entry_read()函數,如果成功,它將返回指定zip條目的內容,否則將返回PHP警告。
語法:string zip_entry_read( $zip_entry, $length )
參數:
該函數接受兩個參數,如下所述。
$zip_entry:這是一個指定zip條目資源的強制參數。
$length:它是一個可選參數,指定要返回的字節數。
返回值:
成功時返回指定zip條目的內容,否則返回PHP警告。
錯誤和異常:
如果zip存檔無效,zip_entry_read()函數將返回ER_OPEN錯誤。
如果zip存檔為空,則zip_entry_read()函數返回ER_NOZIP錯誤
下面的程序演示了PHP中的zip_entry_read()函數:
示例1:
假設zip文件article.zip包含文件:geeks.txt<?php
// 打開zip文件
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
// 讀取zip存檔項
while($zip_entry = zip_read($zip_handle))
{
$resource = zip_entry_open($zip_handle, $zip_entry, "rb");
$file_name = zip_entry_name($zip_entry);
if ($resource == true)
{
// 讀取zip存檔項的內容
$file_content = zip_entry_read($zip_entry);
echo("File: " . $file_name . " successfully opened.
");
echo("File content: " . $file_content);
// 關閉zip歸檔項
zip_entry_close($zip_entry);
}
else
echo("Failed to Open.");
}
// 關閉zip文件
zip_close($zip_handle);
?>
輸出:File: articles/geeks successfully opened.
File content: Welcome to GeeksforGeeks. It is a computer science portal
where you can learn programming.
示例2:
假設zip文件article.zip包含以下文件:
geeks.txt
geeks1.txt<?php
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
while($zip_entry = zip_read($zip_handle))
{
$resource = zip_entry_open($zip_handle, $zip_entry, "rb");
$file_name = zip_entry_name($zip_entry);
if ($resource == true)
{
// 讀取zip存檔項的內容,最多可達150字節
$file_content = zip_entry_read($zip_entry, 150);
echo("File Name: " . $file_name . " is opened Successfully.
");
echo($file_content);
echo("
");
zip_entry_close($zip_entry);
}
else
echo("Failed to Open.");
}
zip_close($zip_handle);
?>
輸出:File Name: articles/geeks is opened Successfully.
Welcome to GeeksforGeeks. It is a computer science portal where you
can learn programming.
File Name: articles/geeks1 is opened Successfully.
A Computer Science portal for geeks. It contains well written, well
thought and well-explained computer science and programming articles,
quizzes and many more.
相關推薦:《PHP教程》
總結
以上是生活随笔為你收集整理的php读取zip文件,php如何读取zip内容?(zip_entry_read函数的使用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 匹配多行,PHP-选择与相关表中
- 下一篇: php 代码线程,php实现多线程代码