Current Location: Home> Latest Articles> How to Read a Single File from a ZIP Archive Using PHP

How to Read a Single File from a ZIP Archive Using PHP

M66 2025-07-07

How to Read a Single File from a ZIP Archive Using PHP

In PHP, if you need to read a specific file from a ZIP archive, you can achieve this by utilizing PHP's handling of ZIP files. This article will show you how to read a single file from a ZIP archive in PHP.

PHP Code Example

Here is an example of how to read a single file from a ZIP archive in PHP:

$handle = fopen('zip://test.zip#test.txt', 'r');

$result = '';

while (!feof($handle)) {

$result .= fread($handle, 8192);

}

fclose($handle);

echo $result;

Using the code above, you can open a ZIP file, read, and display the contents of a specific file. In this example, the file is `test.txt`, and its contents will be output directly.

Conclusion

By using PHP's `fopen` and `fread` functions, you can easily extract the contents of a single file from a ZIP archive. This method can be applied to various use cases, such as file viewing and data extraction.