导航:首页 > 文件处理 > 在线压缩php代码

在线压缩php代码

发布时间:2022-11-12 10:34:42

php 压缩文件夹

php将文件夹打包成zip文件,参考代码如下:

functionaddFileToZip($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=newZipArchive();
if($zip->open('images.zip',ZipArchive::OVERWRITE)===TRUE){
addFileToZip('images/',$zip);//调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close();//关闭处理的zip文件
}

㈡ php在线压缩代码,求教,各位老大帮帮忙

用PHP写一个方法
查找PHP文件中所有的制表符和换行符并替换成空就好了

㈢ php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)

本文实例讲述了php使用pclzip类实现文件压缩的方法。分享给大家供大家参考,具体如下:
使用PclZIp(zip格式)压缩,首先需要下载它的包文件(可点击此处本站下载)。PclZip功能还是蛮强大的,它可以进行压缩和解压,以及一些添加和删除的类的方法等等。当然了这些内容我们都可以在网上查找的到,没必要都得记住。我们只要在需要使用的时候自己可以很快的在网上找到使用方法就可以了。首先我们需要的就是要将下载的库文件进行引入,如
<?php
include('pclzip/pclzip.lib.php');
?>
//括号里面的地址改成自己的pclzip.lib.php
文件所在地址,
//它的所有的功能都在pclzip.lib.php里面
引入之后我们就可以进行使用了,下面是我使用时简单写的一个方法。
<?php
$zipname
=
"test.zip";
//压缩包的名称
$zipnames
=
'/zipfiles/'.$zipname;
//压缩包所在路径
$z
=
new
PclZip($zipnames);
//实例化这个PclZip类
$v_list
=
$z->create('file.txt,data/text.txt,folder');
//将文件进行压缩
if
($v_list
==
0)
{
die("Error
:
".$archive->errorInfo(true));
//如果有误,提示错误信息。
}
?>
这个一个简单的zip压缩就做好了,在这个方法中我们主要就是使用了PclZip里面的PclZip类以及它里面创建压缩包的方法create。从上面的例子我们可以看到下面这个内容
<?php
PclZip($zip_filename);
?>
//里面是该压缩包的名称以及所在路径。并不是把名称写上就可以了。
//如果路径不对是无法找到的。
<?php
PclZip::create($filelist,
[optional
arguments]);
?>
//create方法来创建压缩包。
//$filelist可以用数组包含文件的名称和文件夹名称或者是一个字符串来包含.
//如果是多个文件的话可以用逗号来隔开,如上面的例子。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP操作zip文件及压缩技巧总结》、《php文件操作总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

㈣ PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

三个文件代码如下:
连接数据库:connect.php

<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",mysqli_connect_error());
exit();
}
?>

当然使用一些封装的数据库类也是可以的。

执行SQL语句:test_upload.php

<?php
require("connect.php");
require("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql="insertintoimg(real_img,small_img)values(?,?)";
$result=$sqlconn->prepare($insert_sql);
$result->bind_param("ss",$real_img,$small_img);
$result->execute();
?>

上传图片并压缩:upload_img.php

<?php
//设置文件保存目录
$uploaddir="upfiles/";
//设置允许上传文件的类型
$type=array("jpg","gif","bmp","jpeg","png");

//获取文件后缀名函数
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}

//生成随机文件名函数
functionrandom($length)
{
$hash='CR-';
$chars='';
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i=0;$i<$length;$i++)
{
$hash.=$chars[mt_rand(0,$max)];
}
return$hash;
}

$a=strtolower(fileext($_FILES['filename']['name']));

//判断文件类型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))
{
$text=implode(",",$type);
$ret_code=3;//文件类型错误
$page_result=$text;
$retArray=array('ret_code'=>$ret_code,'page_result'=>$page_result);
$retJson=json_encode($retArray);
echo$retJson;
return;
}

//生成目标文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);
do
{
$filename[0]=random(10);//设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}

while(file_exists($uploadfile));

if(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['filename']['tmp_name']))
{
$ret_code=1;//上传失败
}
else
{//上传成功
$ret_code=0;
}
}
$retArray=array('ret_code'=>$ret_code);
$retJson=json_encode($retArray);
echo$retJson;
}

//压缩图片

$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;

//$pic_width_max=120;
//$pic_height_max=90;
//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

$file_type=$_FILES["filename"]['type'];

functionResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得当前图片大小
$width=imagesx($uploadfile);
$height=imagesy($uploadfile);
$i=0.5;
//生成缩略图的大小
if(($width>$maxwidth)||($height>$maxheight))
{
/*
$widthratio=$maxwidth/$width;
$heightratio=$maxheight/$height;

if($widthratio<$heightratio)
{
$ratio=$widthratio;
}
else
{
$ratio=$heightratio;
}

$newwidth=$width*$ratio;
$newheight=$height*$ratio;
*/
$newwidth=$width*$i;
$newheight=$height*$i;
if(function_exists("imageresampled"))
{
$uploaddir_resize=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}

ImageJpeg($uploaddir_resize,$name);
ImageDestroy($uploaddir_resize);
}
else
{
ImageJpeg($uploadfile,$name);
}
}if($_FILES["filename"]['size'])
{
if($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")
{
//$im=imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/x-png")
{
//$im=imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/gif")
{
//$im=imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
else//默认jpg
{
$im=imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

ImageDestroy($im);
}
}
?>

请按照现实情况更改connect.php,test_upload.php中对应的信息。

望采纳,谢谢。

㈤ PHP等比例压缩图片的实例代码

具体代码如下所示:
/**
*
desription
压缩图片
*
@param
sting
$imgsrc
图片路径
*
@param
string
$imgdst
压缩后保存路径
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//压缩后的图片宽
$new_height
=
$height;//压缩后的图片高
if($width
>=
600){
$per
=
600
/
$width;//计算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
总结
以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP
实现等比压缩图片尺寸和大小实例代码php
gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

㈥ php实现解压缩功能

/*
由于你给我的说明不太清楚,所以可能在有些地方未能合你的本意.
*/

#include "stdafx.h"//如果发生编译错误,请删除此句再试一试
#include <iostream>

using namespace std;

//类CVehicle的申明
class CVehicle
{
public:
CVehicle();//构造函数申明
CVehicle(const CVehicle &);//拷贝构造函数申明
~CVehicle();//析构函数申明

void SetCarNo(const char *);//设置车牌号的成员函数
const char * GetCarNO(void);//获取车牌号的成员函数

void SetTotalPerson(long);//设置载客数的成员函数
void SetTotalWeight(double);//设置总的重量的成员函数
long GetTotalPerson(void);//获取载客数量
double GetTotalWeight(void);//获取总的重量

bool operator == (const CVehicle &);//重载==运算符
bool operator != (const CVehicle &);//重载!=运算符

friend char * GetVehicleID(const CVehicle &);//获取车牌号的友员函数申明

private:
char * p_id;//保存车牌号的成员变量

long total_person;//保存总的载客数的成员变量
double total_weight;//保存总的载重数量的成员变量
};

//类CCar的申明
class CCar: public CVehicle
{
public:
CCar();//构造函数
~CCar();//析构函数

void SetCarriedPerson(long);//设置准载的人数
long GetCarriedPerson(void);//获取准载人数

private:
long carried_person; //保存准载人数
};

class CTruck: public CVehicle
{
public:
CTruck();
~CTruck();

void SetCarriedWeight(double);//设置准载重量
double GetCarriedWeight(void);//获取准载重量

private:
long carried_weight;//保存准载重量的成员变量
};

//类CVehicle的构造函数
CVehicle::CVehicle()
{
p_id = new char[32];//为保存车牌号的成员变量申请32字节内存
p_id[0] = 0;//初始化车牌号为空字符串
total_person = 0;//初始化总的载客数为零个
total_weight = 0;//初始化总的载重吨数为零
};

//类CVehicle的拷贝构造函数
CVehicle::CVehicle(const CVehicle & cv)
{
p_id = new char[32];//为保存车牌号的成员变量申请32字节内存

if (p_id !=NULL )
{
strcpy(p_id,cv.p_id);
total_person = cv.total_person;
total_weight = cv.total_weight;
}
};

//类CVehicle的析构函数
CVehicle::~CVehicle()
{
if (p_id != NULL)
{
delete [] p_id;//释放之前申请的内存
}
};

//设置车牌号的成员函数
void CVehicle::SetCarNo(const char * carno)
{
strcpy(p_id,carno);
};

//获取车牌号的成员函数
const char * CVehicle::GetCarNO(void)
{
return p_id;
};

//设置总的载客数的成员函数
void CVehicle::SetTotalPerson(long tp)
{
total_person = tp;
};

//设置总的载重吨数的成员函数
void CVehicle::SetTotalWeight(double tw)
{
total_weight = tw;
};

//获取总的载客数的成员函数
long CVehicle::GetTotalPerson(void)
{
return total_person;
};

//获取总的载重吨数的成员函数
double CVehicle::GetTotalWeight(void)
{
return total_weight;
};

//重载==运算符
bool CVehicle::operator == (const CVehicle & cv)
{
return (strcmp(cv.p_id,p_id) == 0);
};

//重载!=运算符
bool CVehicle::operator != (const CVehicle & cv)
{
return (strcmp(cv.p_id,p_id) != 0);
};

//类CCar的构造函数
CCar::CCar()
:CVehicle()
{
carried_person = 0;
};

//类CCar的析构函数
CCar::~CCar()
{
//do nothing
};

//类CCar的设置准载人数的成员函数
void CCar::SetCarriedPerson(long cp)
{
carried_person = cp;
SetTotalPerson(cp);
};

//类CCar的获取准载人数的成员函数
long CCar::GetCarriedPerson(void)
{
return carried_person;
};

//类CTruck的构造函数
CTruck::CTruck()
:CVehicle()
{
carried_weight = 0;
};

//类CTruck的析构函数
CTruck::~CTruck()
{
//do nothing
};

//类CTruck的设置准载重量的成员函数
void CTruck::SetCarriedWeight(double cw)
{
carried_weight = cw;
SetTotalWeight(cw);
};

//类CTruck的获取准载重量的成员函数
double CTruck::GetCarriedWeight(void)
{
return carried_weight;
};

//获取车牌号的友员函数
char * GetVehicleID(const CVehicle & cv)
{
return cv.p_id;
};

//在下面编写测试上面定义的类的代码.
//并未写完整,你可以根据你自己的需要添加测试代码.
int main(int argc, char* argv[])
{
CVehicle cv;
cv.SetCarNo("川A5168");
cv.SetTotalPerson(5);
cv.SetTotalWeight(2);

cout<<GetVehicleID(cv)<<endl;

CVehicle cv1(cv);
cout<<(cv1 == cv)<<endl;

cout<<cv1.GetCarNO()<<endl;

return 0;
};

㈦ 如何用php压缩html代码并输出

第一步,你需要对php的设置如下:
php.ini: output_buffering = Off output_handler = ob_gzhandler zlib.output_compression = Off zlib.output_compression_level = -1

第二步,你需要在apache下增加如下设置:

AddOutputFilter DEFLATE html php js css

这样就可以对html php js css进行gzip压缩了。

第三步,你需要使用如下php压缩html并输出到客户端的函数:

function compress_html($string) { return ltrim(rtrim(preg_replace(array("/> *([^ ]*) *</","//","'/\*[^*]*\*/'","/\r\n/","/\n/","/\t/",'/>[ ]+</'), array(">\\1<",'','','','','','><'),$string))); }

上面的这个正则表达式,很强大的哦,经过我本人亲自测试可使用。
通过以上方法,你就可以将你的html代码压缩然后输出给客户端了。不信你可以查看源代码,就是一行,网页瞬间压缩很小。

㈧ php 压缩文件夹

php将文件夹打包成zip文件,参考代码如下:

functionaddFileToZip($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=newZipArchive();
if($zip->open('images.zip',ZipArchive::OVERWRITE)===TRUE){
addFileToZip('images/',$zip);//调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close();//关闭处理的zip文件
}

㈨ PHP在线解压缩软件是什么

终于找到几款好用的php在线解压缩工具了!
如果你的空间不是通过cPanel管理的,那么强力推荐你使用,特别是国内用户。

要解决的问题:
XOOPS程序以及mole的文件通常都很多,一个小站点都有好几千个文件,这种情况下,无论是安装还是备份网站,如果用FTP一个个上传、下载文件,即使你的空间速度很快,还是要花很长时间!!

解决思路:
把文件压缩成一两个压缩包,再上传、下载就能大大节约时间。
上传(安装):在本地先把文件压缩为一两个压缩包,接着用FTP上传,最后用在线解压工具解压。
下载(备份):先用在线解压工具压缩为一两个压缩包,接着下载,最后在本地解压。

㈩ 如何用php解压缩文件

您好,ZIP格式的可以

<?php
//需开启配置php_zip.dll
//phpinfo();
header("Content-type:text/html;charset=gb2312");
functionget_zip_originalsize($filename,$path){
//先判断待解压的文件是否存在
if(!file_exists($filename)){
die("文件$filename不存在!");
}
$starttime=explode('',microtime());//解压开始的时间

//将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到
$filename=iconv("utf-8","gb2312",$filename);
$path=iconv("utf-8","gb2312",$path);
//打开压缩包
$resource=zip_open($filename);
$i=1;
//遍历读取压缩包里面的一个个文件
while($dir_resource=zip_read($resource)){
//如果能打开则继续
if(zip_entry_open($resource,$dir_resource)){
//获取当前项目的名称,即压缩包里面当前对应的文件名
$file_name=$path.zip_entry_name($dir_resource);
//以最后一个“/”分割,再用字符串截取出路径部分
$file_path=substr($file_name,0,strrpos($file_name,"/"));
//如果路径不存在,则创建一个目录,true表示可以创建多级目录
if(!is_dir($file_path)){
mkdir($file_path,0777,true);
}
//如果不是目录,则写入文件
if(!is_dir($file_name)){
//读取这个文件
$file_size=zip_entry_filesize($dir_resource);
//最大读取6M,如果文件过大,跳过解压,继续下一个
if($file_size<(1024*1024*6)){
$file_content=zip_entry_read($dir_resource,$file_size);
file_put_contents($file_name,$file_content);
}else{
echo"<p>".$i++."此文件已被跳过,原因:文件过大,->".iconv("gb2312","utf-8",$file_name)."</p>";
}
}
//关闭当前
zip_entry_close($dir_resource);
}
}
//关闭压缩包
zip_close($resource);
$endtime=explode('',microtime());//解压结束的时间
$thistime=$endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
$thistime=round($thistime,3);//保留3为小数
echo"<p>解压完毕!,本次解压花费:$thistime秒。</p>";
}
$size=get_zip_originalsize('temp/test.zip','temp/');
?>
阅读全文

与在线压缩php代码相关的资料

热点内容
dvd光盘存储汉子算法 浏览:758
苹果邮件无法连接服务器地址 浏览:963
phpffmpeg转码 浏览:672
长沙好玩的解压项目 浏览:145
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:737
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:486
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:383
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:350
风翼app为什么进不去了 浏览:779
im4java压缩图片 浏览:362
数据查询网站源码 浏览:151
伊克塞尔文档怎么进行加密 浏览:893
app转账是什么 浏览:163