
正文
php接口传输数据压缩 php 传输文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP 实现文件压缩解压zip格式
在php中,有时我们需要使用到压缩文件操作,压缩文件可以节省磁盘空间;且压缩文件更小,便于网络传输,效率高,下面我们就来了解php的压缩解压相关操作
在PHP中有一个ZipArchive类,专门用于文件的压缩解压相关操作
在ZipArchive类中主要使用到了如下方法:
第一个参数:要打开的压缩包文件
第二个参数:
ZIPARCHIVE::OVERWRITE 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉
ZIPARCHIVE::CREATE 如果指定的zip文件不存在,则新建一个
ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错
ZIPARCHIVE::CHECKCONS 对指定的zip执行其他一致性测试
上面就是ZipArchive的一些常用方法,下面来一些简单示例
相关问答
Q1: PHP网站上传图片自动压缩,怎么编程啊,求指
这里会使用到三个文件:
connect.php:连接数据库
test_upload.php:执行SQL语句
upload_img.php:上传图片并压缩
三个文件代码如下:
连接数据库:connect.php
?php
$db_host = '';
$db_user = '';
$db_psw = '';
$db_name = '';
$db_port = '';
$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);
$q="set names utf8;";
$result=$sqlconn-query($q);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", 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 = "insert into img (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");
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//生成随机文件名函数
function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$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'];
function ResizeImage($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("imagecopyresampled"))
{
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);
imagecopyresized($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中对应的信息。
望采纳,谢谢。
Q2: php 怎么对url的参数串进行压缩和解压缩
如果参数名和值全部是已知的,那么做一个映射表就可以了。
如果参数的值涉及用户提交的内容,对于过长的内容,最好使用POST,并开启Gzip压缩。
关于URL的长度:
1,普通用户很少通过修改url来实现跳转
2,太短的参数名称就很难理解含义,比如content如果缩短成c,你知道这代表什么意思么?
3,如果说为了便于保存和分享,那么可以考虑short url的处理方式
4,url的长度对性能的影响微乎其微,除非是超长文本,那就是程序设计的问题了
5,如果说刻意追求极短的url,还要进行压缩和解压缩步骤,真的有点画蛇添足了
Q3: 如何用PHP上传RAR压缩包同时解压到指定目录
?php
header("content-type:text/html;charset=utf-8");
$path = getcwd();//获取当前系统目录
if($_POST['sub'])
{
$tname = $_FILES["ufile"]["tmp_name"];
$fname = $_FILES["ufile"]["name"];
move_uploaded_file($tname,$fname);
$obj=new com("wscript.shell");//使用PHP预定义的Com组件加载Shell,加载wscript.shell用来执行dos命令的组件
$obj-run("winrar x $path\\".$fname." ".$path,1,true);//所要执行的命令
unlink($fname);//解压后删除已上传的压缩文件
}
?
form action="" method="post" enctype="multipart/form-data"
选择要上传的文件:input type="file" name="ufile"
input type="submit" name="sub" value="上传压缩文件并解压"
/form
关于php接口传输数据压缩和php 传输文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





