導航:首頁 > 文件處理 > 在線壓縮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