Home > Article > Backend Development > How to use PHP to display images in a compressed package without decompressing it
I believe that everyone knows how to use PHP to operate ZIP compressed package files. So in some special cases, we need to see the files in the compressed package without decompressing it. How do we do this? Today I will bring you such a method, let’s take a look at it together.
$zip_path = '12.zip';//压缩包实际路径 $zip = zip_open($zip_path);//利用zip_open函数打开压缩包 while ($re = zip_read($zip)) {//读依次读取包中的文件 if (!zip_entry_filesize($re)) break;//如果文件大小为0退出操作 $entry_zp = zip_entry_open($zip,$re,'rb');//读取包中文件 $ext = pathinfo(zip_entry_name ($re),PATHINFO_EXTENSION);//获取图片文件扩展名 $buf = zip_entry_read($re,zip_entry_filesize($re));//读取文件二进制数据 echo sprintf('<img src="data:image/%s;base64,%s">', $ext, base64_encode($buf));//利用base64_encode函数转换读取到的二进制数据并输入输出到页面中 zip_entry_close($re);//关闭打开的压缩包中的文件 } zip_close($zip);//关闭压缩包文件
The most important thing about this code is to use base64_encode to convert the binary data of the image file into browser-readable Base64 image data
Note: When using PHP's ZIP FILE series functions, please confirm that the php_zip.dll extension library has been enabled in your PHP.ini file, and whether there is php_zip in the ext folder in the PHP installation directory. dll (I am using Windows system). If php_zip.dll does not exist, you can go to the following PHP official PECL expansion package site to obtain
PHP official PECL expansion package site address: http://pecl.php.net/package/zip
Select the compressed package corresponding to your PHP version;
Under Windows, you can directly drag and drop the php_zip.dll file in the compressed package to the ext folder in the PHP installation directory;
Linux system needs to be compiled Then modify the configuration file of PHP
Attach the basic functions of PHP to operate Zip File
zip_close() 关闭 ZIP 文件。 4 zip_entry_close() 关闭 ZIP 文件中的一个项目。 4 zip_entry_compressedsize() 返回 ZIP 文件中的一个项目的被压缩尺寸。 4 zip_entry_compressionmethod() 返回 ZIP 文件中的一个项目的压缩方法。 4 zip_entry_filesize() 返回 ZIP 文件中的一个项目的实际文件尺寸。 4 zip_entry_name() 返回 ZIP 文件中的一个项目的名称。 4 zip_entry_open() 打开 ZIP 文件中的一个项目以供读取。 4 zip_entry_read() 读取 ZIP 文件中的一个打开的项目。 4 zip_open() 打开 ZIP 文件。 4 zip_read() 读取 ZIP 文件中的下一个项目。 4
I believe you have read these cases After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!
Related reading:
Detailed explanation of examples of binary search and binary search in java algorithm
Detailed explanation of binary search in javascript _javascript skills
javascript Find the position of characters in an array by half (ordered list)_javascript skills
The above is the detailed content of How to use PHP to display images in a compressed package without decompressing it. For more information, please follow other related articles on the PHP Chinese website!