PHP online file and folder compression instance Code _php tutorial

Source: Internet
Author: User
Note This PHP tutorial file compression Code he would like to use the Zip.exe file Oh, so we have to take out the Zip.exe compressed files out OH.

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 Pack");
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 to Copyright (). "NNN";
}


function Copyright ()
{
Return "

Please delete this file immediately after use to avoid being discovered by others! N
. "
N
. " N
. "

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

N
. " N
. "N
. N
. "" ;
}

?>


http://www.bkjia.com/PHPjc/444927.html www.bkjia.com true http://www.bkjia.com/PHPjc/444927.html techarticle Note This php tutorial file compression Code he would like to use the Zip.exe file Oh, so we have to take the Zip.exe compressed files out Oh. php if (! IsSet ($_get[' dirname ')) {Show_input_f ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.