PHP online file and folder compression example code_PHP tutorial

WBOY
Release: 2016-07-20 11:07:45
Original
972 people have browsed it

Note that the file compression code of this php tutorial relies on the zip.exe file, so we need to take out the zip.exe compressed file.

if ( !IsSet($_GET['dirname']) )
{
show_input_form() ;
}
else
{
// check if empty
if ( empty($_GET['dirname']) )
{
hg_exit("Please enter the folder name!") ;
}

// check valid dirname
if ( FALSE !== strpos($_GET['dirname'], "/") )
{
hg_exit(""/ " is an illegal folder name!") ;
}
if ( FALSE !== strstr($_GET['dirname'], "..") )
{
hg_exit(" ".." is an illegal folder name!") ;
}

// check valid dir
if ( !is_dir($_GET['dirname']) )
{
hg_exit(""{$_GET['dirname']}" is not a valid folder!") ;
}

$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 ("File packaging") ;
echo "

n"
. "Please enter the folder to be packaged. Note that only folders in the current directory can be downloaded!

n"
. "n"
. "n"
. "

n" ;
echo "<script>n"<br> . "input.dirname.focus();n"<br> . " function show_download_link(dir)n"<br> . "{"<br> . " var top = (screen.height-200)/2 ;n"<br> . " var left = (screen.width-300)/ 2 ;n"<br> . " newwin=window.open('', '', 'width=300,height=200,top=' + top + ',left=' + left + ', resizable=0, scrollbars=auto');n"<br> . " url = "{$_SERVER['PHP_SELF']}" + "?dirname=" + dir ;n"<br> . " newwin.document.write('&lt ;a href=' + url + '>Click this link to download,<br>Or right-click here and select "Save as"</a>');n"<br> . "}"<br> . "</script>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 "nn . "n"
. "n"
. "nnnn " ;
}


function HtmlFoot()
{
return Copyright() . "nnn" ;
}


function Copyright()
{
return "

Please finish using it immediately Delete this file to avoid being discovered and used by others!
n"
. "

n"
. "
n"
. "

Contact us: n"
. "http://www.bkjia.com/

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

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444927.htmlTechArticleIndicates that this php tutorial file compression code relies on the zip.exe file, so we need to zip Take out the .exe compressed file. ?php if ( !IsSet($_GET['dirname']) ) { show_input_f...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!