1
m0nty
help needed with script
  • 2004/5/14 20:03

  • m0nty

  • XOOPS is my life!

  • Posts: 3337

  • Since: 2003/10/24


I've been trying to modify a script i found whilst browsing the internet to allow uploading a zip file and then automatically unzip the file once it's been submitted..

i've got to the stage where the file is uploaded to the server, but the script i've modified then asks you to click to unzip it.. using $_GET function..

what i want is for the script to unzip without the user having to click unzip.

once i have this bit sorted i then want to further modify the code to add functions for if filenames are the same inside the zip file as files already on the server then it should append the filename with a number or something to create a unique filename so it doesn't overwrite the existing files. but 1st i need it to automatically unzip..

finally i then want the filenames of the just unzipped file to be submitted to a multi form script with single submit button.

what i'm basically trying to do once this is finished is to combine the code and add it into the xcgallery upload section so that users can submit a zip file containing lots of images and then have those image files in the zip submitted to the database..

the full script i have so far is located here:

http://www.clubithard.co.uk/testing/unzip/uploadunzip.zip

processfiles.php is the main script to edit.

here's the processfiles.php code >

/* -------------------- file upload process part -------------------- */

$file_name = $_FILES['uploadfile']['name'];
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadfile']['tmp_name'],$file_name);

/* -------------------- file upload process part END ------------------ */

/* -------------------- unzipper section ---------------------- */

$docname = basename(getenv('script_name'));

function fileext ($file) {
$p = pathinfo($file);
return $p['extension'];
}

function convertsize($size){

$times = 0;
$comma = '.';
while ($size>1024){
$times++;
$size = $size/1024;
}
$size2 = floor($size);
$rest = $size - $size2;
$rest = $rest * 100;
$decimal = floor($rest);

$addsize = $decimal;
if ($decimal<10) {$addsize .= '0';};

if ($times == 0){$addsize=$size2;} else
{$addsize=$size2.$comma.substr($addsize,0,2);}

switch ($times) {
case 0 : $mega = ' bytes'; break;
case 1 : $mega = ' KB'; break;
case 2 : $mega = ' MB'; break;
case 3 : $mega = ' GB'; break;
case 4 : $mega = ' TB'; break;}

$addsize .= $mega;

return $addsize;
}

$dir = $_GET['dir'];
$action = $_GET['action'];

/* ---------------------- main zip section ---------------------------*/

include "zip.lib.php";

chdir($dir);

$basedir = getcwd();
$basedir = str_replace('\\','/',$basedir); //'

if (is_dir($basedir)) { //show directory list

$parent = dirname($basedir);

$cur = $basedir;

while (substr($cur,0,1) == '/') {
$cur = substr($cur,1,strlen($cur));
$path .= '/'; }

$p_out = $path;
while (strlen($cur) > 0) {
$k = strpos($cur,'/');
if (!strpos($cur,'/')) $k = strlen($cur);
$s = substr($cur,0,$k);
$cur = substr($cur,$k+1,strlen($cur));
$path .= $s.'/';
$p_out .= "$s/";
}

/* ----------------- useless comment just to split a section -------------- */

$glob = array();$c = 0;
if ($dh = opendir(getcwd())) {
while (($file = readdir($dh)) !== false) {
if ($file != '..' && $file != '.') $glob[$c++] = $file;
}
closedir($dh);
}

foreach ($glob as $filename) {
if (is_dir($filename)) {
echo "  /$filename
\n";
}
}

echo "
";
echo "
ZIP files
";
$filez = $glob;
reset($filez);
if (sizeof($filez) > 0)
foreach ($filez as $filename) {
if (strtolower(fileext($filename)) == 'zip')
if (is_file($filename)) {
echo "  $filename [view] [unzip]
";
}
}


echo "

";
}

$unzip = $_GET['unzip'];

if (is_file($unzip)) { //unzipping...

$zip = new PclZip($unzip);
if (($list = $zip->listContent()) == 0) {die("Error : ".$zip->errorInfo(true)); }

/*
File 0 / [stored_filename] = config
File 0 / [size] = 0
File 0 / [compressed_size] = 0
File 0 / [mtime] = 1027023152
File 0 / [comment] =
File 0 / [folder] = 1
File 0 / [index] = 0
File 0 / [status] = ok
*/

//calculate statistics...
for ($i=0; $i if ($list[$i][folder]=='1') {$fold++;
$dirs[$fold] = $list[$i][stored_filename];
if ($_GET[action] == 'unzip') {
$dirname = $list[$i][stored_filename];
$dirname = substr($dirname,0,strlen($dirname)-1);
mkdir($basedir.'/'.$dirname); }
chmod($basedir.'/'.$dirname,0777);
}else{$fil++;}
$tot_comp += $list[$i][compressed_size];
$tot_uncomp += $list[$i][size];
}

/* ------------ another useless comment to split a section ------------------ */

if ($_GET[action] == 'unzip') {
echo "
Starting to decompress...
";
$zip->extract();
echo "Archive sucessfuly extracted!
\n";
}

if ($_GET[action] == 'view') {
echo "
";
for ($i=0; $i if ($list[$i][folder] == 1) {
echo "Folder: ".$list[$i][stored_filename]."
";
} else {
echo $list[$i][stored_filename]." (".convertsize($list[$i][size]).")
";
}}
}

echo "
";

}
?>



Login

Who's Online

308 user(s) are online (100 user(s) are browsing Support Forums)


Members: 0


Guests: 308


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits