Php online file and folder compression instance code

Source: Internet
Author: User
Tags php online php tutorial sprintf strlen zip

Note that this PHP Tutorial file compression code is used to help zip.exe files, so we need to extract zip.exe compressed files.

<? Php

If (! IsSet ($ _ GET ['dirname'])
{
Show_input_form ();
}
Else
{
// Check if empty
If (empty ($ _ GET ['dirname'])
 {
Hg_exit ("Enter the folder name! ");
 }

// Check valid dirname
If (FALSE! = Strpos ($ _ GET ['dirname'], "/")
 {
Hg_exit ("/" is an invalid folder name! ");
 }
If (FALSE! = Strstr ($ _ GET ['dirname'], "...")
 {
Hg_exit ("" .. "is an invalid 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 "<form name =" input "> n"
. "Enter the folder to be packaged. Note that only the folder in the current directory can be downloaded! <P> n"
. "<Input name =" dirname "> n"
. "<Input type =" button "value =" OK "onClick =" show_download_link (dirname. value); "> n"
. "</Form> n ";
Echo "<script> 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 ('','', 'width = 300, height = 200, top = '+ top +', left = '+ left +', resizable = 0, scrollbars = auto'); n"
. "Url =" {$ _ SERVER ['php _ SELF ']} "+ "? Dirname = "+ dir; n"
. "Newwin.doc ument. write ('<a href =' + url + '> click this link to download, <br> or right-click here and choose "save as" </a>'); n"
."}"
. "</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 "Echo HtmlFoot ();
Exit;
}


Function HtmlHead ($ title)
{
Return ". "<Meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "> n"
. "<Style type =" text/css tutorial "> n"
. "Body, input, td {font: 12px verdana} n"
. "</Style> n"
. "</Head> nn <body> nn ";
}


Function HtmlFoot ()
{
Return Copyright (). "n </body> nn }


Function Copyright ()
{
Return "<center> <font size =" 5 "face =" _ GB2312 "color =" red "> delete this file immediately after use, to avoid being discovered and used by others! </Font> </center> n"
<Br> . "<Center> n"
. "<P style =" font-family: verdana; font-size: 12px "> Contact us: n"
. "<A href =" http://www.111cn.net/"target =" _ blank "> http://www.111cn.net/</a> </p> n"
. "</Center> n"
. "</Body> n"
. "N"
. "</Html> ";
}

?>

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.