login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Overige scripts > Unzip functie

Unzip functie

Auteur: lucasvdh - 06 januari 2010 - 19:36 - Gekeurd door: Koen - Hits: 3082 - Aantal punten: (0 stemmen)




Even een simpele unzip functie gemaakt..
voorbeeldje van een implementatie staat er bij ;)

Plz let me know als er nog wat bugs in zitten..
lucas@hb-webdesign.nl

Code:
  1. <?php
  2. /*
  3. #################################
  4. # AUTHOR: Lucas vd have #
  5. # VERSION: v1.0 #
  6. # DATE: 06-01-10 #
  7. # WEBSITE: www.hb-webdesign.nl #
  8. #################################
  9. */
  10.  
  11. // VOORBEELD ALS JE WIL UNZIPPEN AAN DE HAND VAN $_GET
  12. if(isset($_GET['unzip'])){
  13. $info = unzip($_GET['unzip'],"destinationFolder/");
  14. if(is_array($info)) {
  15. echo "Extracted successfully<br /><br />";
  16. echo "Folders:<br />";
  17. for($i=0; $i<count($info["folders"]); $i++){
  18. echo $info["folders"][$i]."<br />";
  19. }
  20. echo "<br />Files extracted:<br />";
  21. for($i=0; $i<count($info["files"]); $i++){
  22. echo $info["files"][$i]." - ".$info["size"][$i]." bytes<br />";
  23. }
  24. }
  25. else {
  26. echo "<font color='red'>Error : ".$info."</font><br />";
  27. }
  28. }
  29. // VOORBEELD
  30.  
  31.  
  32.  
  33. /*
  34. Function unzip
  35. Unzip a *.zip file and save files to server.
  36.  
  37. @param $file path to *.zip file
  38. @param $destination destination folder (not required)
  39.  
  40. @return array() [files] //all files extracted
  41. [size] //size of extracted file
  42. [compressionMethod] //compressionmethod of file
  43. [folders] //all folders extracted
  44. */
  45. function unzip($file,$destination = NULL)
  46. {
  47. if(!is_dir($destination) && $destination!=NULL) // if the destination does not exist and is not NULL, return error
  48. return "Destination does not exist";
  49. if(!is_file($file)) // if the file does not exist return error
  50. return "File does not exist";
  51. $filetype = mime_content_type($file);
  52. if ($filetype=="application/zip") {
  53.  
  54. $zip = zip_open($file); // open zip
  55.  
  56. // initialize arrays for optional data
  57. $fileArray = array();
  58. $folderArray = array();
  59. $sizeArray = array();
  60. $compressionMethodArray = array();
  61.  
  62. while ($zip_entry = zip_read($zip)) { // read entire zip
  63.  
  64. // initialize variables
  65. $name = zip_entry_name($zip_entry);
  66. $size = zip_entry_filesize($zip_entry);
  67. $compressed_size = zip_entry_compressedsize($zip_entry);
  68. $compressionMethod = zip_entry_compressionmethod($zip_entry);
  69.  
  70.  
  71. if (zip_entry_open($zip, $zip_entry, "r")) {
  72. $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  73. $lastchar = $name[strlen($name)-1];
  74. if($lastchar=='/'){ // if $name is a dir, create a dir with permissions 0777
  75. if(@mkdir($destination.$name, 0777))
  76. array_push($folderArray,$name);
  77. else
  78. return "Unable to create directory"; // an error occurred
  79. }
  80. else { // if $name is NOT a dir, create file
  81. $f = fopen($destination.$name,'w'); // create file
  82. if(fwrite($f,$content)) { // write content to file
  83. array_push($fileArray,$name); // push filename to array
  84. array_push($sizeArray,$size); // push filesize to array
  85. array_push($compressionMethodArray,$compressionMethod); // push compressionMethod to array
  86. }
  87. else {
  88. return "Unable to write file"; // an error occurred
  89. }
  90. fclose($f); // close file
  91. zip_entry_close($zip_entry);
  92. }
  93. }
  94. }
  95. zip_close($zip); // close zip
  96.  
  97. for($i=0;$i<count($folderArray); $i++){
  98. chmod($folderArray[$i],0755); // set chmod back to 0755 so it can't be edited by everyone.
  99. }
  100. // everything is completed, return the array with optional data
  101. return array("files" => $fileArray, "folders" => $folderArray, "size" => $sizeArray, "compressionMethod" => $compressionMethodArray);
  102. }
  103. else {
  104. return "Unable to extract file (not a *.zip file)."; // an error occurred
  105. }
  106. }
  107. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (1)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.033s