作者:小小伟 | 来源:互联网 | 2023-09-25 13:10
注明这款php文件压缩代码他要借助于zip.exe文件,所以我们要把zip.exe压缩文件给拿出来,实例代码如下:?phpif(!IsSet($_GET[#39;dirname#3
php在线文件与文件夹压缩实例代码
注明这款php文件压缩代码他要借助于zip.exe文件,所以我们要把zip.exe压缩文件给拿出来,实例代码如下:
if ( !IsSet( $_GET [ 'dirname' ]) )
{
show_input_form() ;
}
else
{
// check if empty
if ( empty empty ( $_GET [ 'dirname' ]) )
{
hg_exit( "请输入文件夹名!" ) ;
}
// check valid dirname
if ( FALSE !== strpos ( $_GET [ 'dirname' ], "/" ) )
{
hg_exit( "" / " 是非法的文件夹名!" ) ;
}
if ( FALSE !== strstr ( $_GET [ 'dirname' ], ".." ) )
{
hg_exit( "" .. " 是非法的文件夹名!" ) ;
}
// check valid dir
if ( ! is_dir ( $_GET [ 'dirname' ]) )
{
hg_exit( "" { $_GET [ 'dirname' ]} " 不是一个有效的文件夹!" ) ;
}
$szData = "" ;
$szInfo = "" ;
$file_count = @ZipDir( $_GET [ 'dirname' ], & $szData , & $szInfo ) ;
$info_size_16byte = @sprintf( "%016d" , @ strlen ( $szInfo )) ;
$szData = @sprintf( "%016d" , $file_count ) . $info_size_16byte . $szInfo . $szData ;
$filename = $_GET [ 'dirname' ] . ".dat" ;
if ( function_exists(gzencode) )
{
$szData = gzencode( $szData ) ;
$filename .= ".gz" ;
}
Header( "Content-type: application/octet-stream" );
Header( "Accept-Ranges: bytes" );
Header( "Accept-Length: " . strlen ( $szData ));
Header( "Content-Disposition: attachment; filename=$filename" );
echo $szData ;
}
function show_input_form()
{
echo HtmlHead( "文件打包" ) ;
echo "n"
. "请输入要打包的文件夹,注意,仅当前目录下的文件夹才可以下载!n"
. "n"
. "n"
. "n" ;
echo "n"
. "input.dirname.focus();n"
. "function show_download_link(dir)n"
. "{"
. " var top = (screen.height-200)/2 ;n"
. " var left = (screen.width-300)/2 ;n"
. " newwin=window.open('', '', '[dir]n" ;
$file_count = 0 ;
$hDir = OpenDir( $szDirName ) ;
while ( $file = ReadDir( $hDir ) )
{
if ( $file == "." || $file == ".." ) continue ;
$szCurFile = "$szDirName/$file" ;
if ( Is_Dir ( $szCurFile ) )
{
$file_count += ZipDir( $szCurFile , & $szData , & $szInfo ) ;
}
else if ( Is_File ( $szCurFile ) )
{
$hCurFile = fopen ( $szCurFile , "rb" ) ;
$size = filesize ( $szCurFile ) ;
$szStream = fread ( $hCurFile , $size ) ;
fclose( $hCurFile ) ;
$file_count ++ ;
// write info
$szInfo .= "$szCurFile|$sizen" ;
// write data
$szData .= $szStream ;
}
}
// write dir footer
$szInfo .= "$szDirName|[/dir]n" ;
return $file_count ;
}
function hg_exit( $str )
{
echo HtmlHead( "Error, exit!" ) ;
echo "
" . $str . "
" ;
echo HtmlFoot() ;
exit ;
}
function HtmlHead( $title )
{
return "nnn"
. "n"
. "n"
. "nnnn" ;
}
function HtmlFoot()
{
return Copyright() . "nnn" ;
}
function Copyright()
{
return "使用完请立即删除本文件,以避免被其它人发现使用!n"
. "
n"
. "n"
. "Contact us: n"
. "http://www.phpfensi.com/
n"
. "n"
. "n"
. "n"
. "" ;
}
?>