A. 我的php怎么变ZIP
在要打开的PHP文件上点击鼠标右键,更改打开方式为浏览器,试一试!
B. 怎么判断ziparchive php扩展是否打开
1.文件下载
header("Content-type: text/html; charset=utf-8"); //设置头信息
if (!file_exists($file_dir.$name)){ //判断是否存在某个文件
echo "File not found!"; //如果不存在就提示用户文件未找到
} else {
$file = fopen($file_dir.$name,"r"); //否则就读取文件
Header("Content-type: application/octet-stream"); //设置浏览器下载需要的头,告诉客户端的浏览器服务端返回的文件形式 是一个下载文件
Header("Accept-Ranges: bytes"); //告诉客户端浏览器返回的文件大小是按照字节进行计算的
Header("Accept-Length: ".filesize($file_dir . $name)); //告诉浏览器返回的文件大小
Header("Content-Disposition: attachment; filename=".$name); //:告诉浏览器返回的文件的名称
echo fread($file, filesize($file_dir.$name)); //按字节读取文件
fclose($file);//关闭文件资源
}
2.文件压缩:
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,
addEmptyDir() 添加一个新的文件目录
addFile() 将文件添加到指定zip压缩包中。
ddFromString()添加的文件同时将内容添加进去
open() 打开一个zip压缩包
close()关闭ziparchive
extractTo()将压缩包解压
getStatusString()返回压缩时的状态内容,包括错误信息,压缩信息等等
deleteIndex()删除压缩包中的某一个文件,如:deleteIndex(0)删除第一个文件
deleteName()删除压缩包中的某一个文件名称,同时也将文件删除。
注意点:使用open方法的时候,第二个参数$flags是可选的,$flags用来指定对打开的zip文件的处理方式,共有四种情况
1. ZIPARCHIVE::OVERWRITE 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉
2.ZIPARCHIVE::CREATE 如果指定的zip文件不存在,则新建一个
3. ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错
4. ZIPARCHIVE::CHECKCONS
一、解压缩zip文件
$zip=new ZipArchive;//新建一个ZipArchive的对象
if($zip->open('test.zip')===TRUE){
$zip->extractTo('images');//假设解压缩到在当前路径下images文件夹内
$zip->close();//关闭处理的zip文件
}
二、将文件压缩成zip文件
$zip=new ZipArchive;
if($zip->open('test.zip',ZipArchive::OVERWRITE)===TRUE){
$zip->addFile('image.txt');//假设加入的文件名是image.txt,在当前路径下
$zip->close();
}
三、文件追加内容添加到zip文件
$zip=new ZipArchive;
$res=$zip->open('test.zip',ZipArchive::CREATE);
if($res===TRUE){
$zip->addFromString('test.txt','file content goes here');
$zip->close();
echo 'ok';
}else{
echo 'failed';
}
四、将文件夹打包成zip文件
function addFileToZip($path,$zip){
$handler=opendir($path); //打开当前文件夹由$path指定。
while(($filename=readdir($handler))!==false){
if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
addFileToZip($path."/".$filename, $zip);
}else{ //将文件加入zip对象
$zip->addFile($path."/".$filename);
}
}
}
@closedir($path);
}
$zip=new ZipArchive();
if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){
addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
}
3.php处理flash扩展:
ming库:
<?php
$f = new SWFFont( '_sans' ); //创建指向一个内置字体(_sans)的指针
$t = new SWFTextField(); //创建文本字段
$t->setFont( $f ); //设定字体
$t->setColor( 0, 0, 0 );//颜色
$t->setHeight( 400 ); //大小,
$t->addString( 'Hello World' ); //提供一些文本内容(“Hello World”)
$m = new SWFMovie(); //创建了一个 SWFMovie 对象并设定其尺寸
$m->setDimension( 2500, 800 );
$m->add( $t ); //向动画中添加了文本元素并将动画保存到文件中。
$m->save( 'hello.swf' ); //在本地保存为 hello.swf
?>
打开浏览器输入 hello.swf 就可以看到了哦。
header( 'Content-type: application/x-shockwave-flash' );
$m = new SWFMovie();
$m->setDimension( 300, 300 );
$s = new SWFShape();
$s->setLine( 5, 0, 0, 0 );
$s->movePenTo( -100, -100 );
$s->drawLineTo( 100, 100 );
$ts = $m->add( $s );
$ts->moveTo( 150, 150 );
for( $i = 0; $i < 100; $i++ ) {
$ts->rotate( 10 );
$m->nextframe();
}
$m->save( 'rotate.swf' );
从 -100, -100 到 100, 100 画了一条直线。这将把直线的中心放在坐标 0,0 处。这样,当我在旋转图形时,直线的中心将发生旋转。
当我向动画中添加图形时,将移动返回到框架中心的 SWFDisplayItem。然后用 rotate() 方法使它旋转并每旋转一周就增大其框架。
<?php
$img = new SWFBitmap(file_get_contents( 'megan.jpg' ));
$s = new SWFShape();
$imgf = $s->addFill( $img );
$s->setRightFill( $imgf );
$s->movePenTo( 0, 0 );
$s->drawLineTo( $img->getWidth(), 0 );
$s->drawLineTo( $img->getWidth(), $img->getHeight() );
$s->drawLineTo( 0, $img->getHeight() );
$s->drawLineTo( 0, 0 );
$m = new SWFMovie();
$m->setDimension( $img->getWidth() * 2, $img->getHeight() * 2 );
$is = $m->add( $s );
$is->moveTo( $img->getWidth() / 2, $img->getHeight() / 2 );
for( $i = 0; $i < 10; $i++ )
{
$is->skewx( 0.02 );
$is->skewy( -0.03 );
$m->nextframe();
}
$m->save( 'image.swf' );
C. 如何安装 php 的zip模块
zip 是php的一个扩展,用于支持zip文件压缩和解压
按照下面的步骤配置:
1. 用记事本编辑你的 php.ini 文件,搜索 zip.dll 然后把这一行前面的 ; (分号)去掉,保存 php.ini 注意,这一行的上面应该有很多诸如 ;php_***.dll 的,否则搜索的位置不正确,再次搜索
2. 重新启动你的 WEB 服务器。IIS直接在 开始 运行 里面输入 iisreset, Apache 通过管理器先停止再启动即可
D. PHP读取zip文件的方法示例
本文实例讲述了PHP读取zip文件的方法。分享给大家供大家参考,具体如下:
<?php
$zip
=
zip_open("111.zip");
if
($zip)
{
while
($zip_entry
=
zip_read($zip))
{
echo
"Name:
"
.
zip_entry_name($zip_entry)
.
"n";
echo
"Actual
Filesize:
"
.
zip_entry_filesize($zip_entry)
.
"n";
echo
"Compressed
Size:
"
.
zip_entry_compressedsize($zip_entry)
.
"n";
echo
"Compression
Method:
"
.
zip_entry_compressionmethod($zip_entry)
.
"n";
if
(zip_entry_open($zip,
$zip_entry,
"r"))
{
echo
"File
Contents:n";
$buf
=
zip_entry_read($zip_entry,
zip_entry_filesize($zip_entry));
echo
"$buf\n";
zip_entry_close($zip_entry);
}
echo
"n";
}
zip_close($zip);
}
?>
运行效果截图如下:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP操作zip文件及压缩技巧总结》、《php文件操作总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
E. PHP如何开启ZIP模块
第一步、找到php.ini文件
第二部、用记事本打开
第三步、使用ctrl键+F键搜索;extension=php_zip.dll
第四步、去除extension前面的分号“;”,如extension=php_zip.dll
第五步、保存重启Apache或其他服务器。
如图:
F. PHP怎么解压ZIP文件
?php
$zip=zip_open("/tmp/test2.zip");
if($zip){
while($zip_entry=zip_read($zip)){
echo"Name:".zip_entry_name($zip_entry)." ";
echo"ActualFilesize:".zip_entry_filesize($zip_entry)." ";
echo"CompressedSize:".zip_entry_compressedsize($zip_entry)." ";
echo"CompressionMethod:".zip_entry_compressionmethod($zip_entry)." ";
if(zip_entry_open($zip,$zip_entry,"r")){
echo"FileContents: ";
$buf=zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));
echo"$buf ";
zip_entry_close($zip_entry);
}
echo" ";
}
zip_close($zip);
}
?>
G. 怎样用php压缩解压rar,zip文件
要用PHP压缩解压文件,常用的方法是调用命令行去执行解压缩操作
可以用exec()
、system()等函数调用shell命令
Linux下解压缩命令是tar
[-cxtzjvfpPN]
文件与目录,tar命令可以压缩解压.tar、.gz、.tar.gz、.tgz、.bz2、.tar.bz2、.Z、.tar.Z、.zip这些类型的文件
Linux下默认无法使用rar格式的,要另外安装RAR
for
Linux,然后使用rar和unrar命令解压缩rar格式的压缩文件
H. 如何用PHP创建一个加密的zip压缩文件
/* creates a compressed zip file */function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files foreach($valid_files as $file) { $zip->addFile($file,$file); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! $zip->close(); //check to make sure the file exists return file_exists($destination); } else { return false; }}
I. 如何通过php实现zip文件解压操作
rar文件解压php没有直接支持的,不过可以通过下载将非线程安全的dll然后扔到php的ext目录下,之后按照下面的步骤操作即可。
打开php.ini.
加一行 extension=php_rar.dll
重启web服务器 和php
复制代码
代码如下:
public function _unzip($fileName,$extractTO){
$fileName = iconv('utf-8','gb2312',"upload/zip/8月.rar");
// echo $fileName . '</br>';
$extractTo = "upload/zip/TEST/";
$rar_file = rar_open($fileName) or die('could not open rar');
$list = rar_list($rar_file) or die('could not get list');
// print_r($list);
foreach($list as $file) {
$pattern = '/\".*\"/';
preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE);
$pathStr=$matches[0][0];
$pathStr=str_replace("\"",'',$pathStr);
// print_r($pathStr);
$entry = rar_entry_get($rar_file, $pathStr) or die('</br>entry not found');
$entry->extract($extractTo); // extract to the current dir
}
rar_close($rar_file);
}