php線上檔案與檔案夾壓縮執行個體代碼_PHP教程

來源:互聯網
上載者:User
註明這款php教程檔案壓縮代碼他要藉助於zip.exe檔案哦,所以我們要把zip.exe壓縮檔給拿出來哦。

if ( !IsSet($_GET['dirname']) )
{
show_input_form() ;
}
else
{
// check if empty
if ( 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" ;
echo "n" ;
echo HtmlFoot() ;
}


function ZipDir($szDirName, &$szData, &$szInfo)
{
// write dir header
$szInfo .= "$szDirName|[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.bkjia.com/

n"
. "n"
. "n"
. "n"
. "" ;
}

?>


http://www.bkjia.com/PHPjc/444927.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444927.htmlTechArticle註明這款php教程檔案壓縮代碼他要藉助於zip.exe檔案哦,所以我們要把zip.exe壓縮檔給拿出來哦。 ?php if ( !IsSet($_GET['dirname']) ) { show_input_f...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.