zip – PHP – pclzip.lib troubles – Education Career Blog

Dunno if someone use this library to make (server-side) a zip files. Here you can see my code :

$i=0;
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
    $title$i=$row1." - ".$row2;

    // i remove some chars, because they can create confusion with the filesystem
    $titleontxt=$title$i;
    $title$i=str_replace("/", "", $title$i);
    $title$i=str_replace(":", "", $title$i);
    $title$i=str_replace("*", "", $title$i);      
    $title$i=str_replace("?", "", $title$i);      
    $title$i=str_replace('"', "", $title$i);
    $title$i=str_replace("<", "", $title$i);
    $title$i=str_replace(">", "", $title$i);
    $title$i=str_replace("|", "", $title$i);                  
    $title$i=str_replace(",", "", $title$i);

    $trackid=$row0;
    mkdir("./temp/".$_SESSION'nickname', 0700);
    $file = fopen("./temp/".$_SESSION'nickname'."/".$title$i.".txt", "w");
    $fwrite = fwrite($file, $titleontxt."\r\n");

    $j=0;
    $fwrite = fwrite($file, "\r\n single side");
    $fwrite = fwrite($file, "\r\n there is a single line");


    fclose($file);
    $i++;
}

include_once("./lib/pclzip.lib.php");
$data=date("Y-m-d");
$zipstring="./temp/".$_SESSION'nickname'."/".$trackid."-GTW-Tracklist.zip";
$filezip=new PclZip($zipstring);

for($i=0; $i<sizeof($title); $i++) {
        // inserisce i txt nel file zip
        $v_list = $filezip->add("./temp/".$_SESSION'nickname'."/".$title$i.".txt", PCLZIP_OPT_REMOVE_ALL_PATH);
        if ($v_list == 0) { 
            die("Error : ".$filezip->errorInfo(true)); 
        } 
}

for($i=0; $i<sizeof($title); $i++) {
    unlink("./temp/".$_SESSION'nickname'."/".$title$i.".txt");
}

header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$zipstring"); 
header("Content-Description: Backup"); 
header("Content-Length: ".filesize($zipstring)); 
readfile($zipstring);     
unlink($zipstring);
rmdir("./temp/".$_SESSION'nickname');
exit;

Anyway, when i try to use this library i have 2 problems :

1 – when i need to make a zip, i use to create a folder called /temp/username . Then, i make that zip. On firefox, when i send the file at the client, it add at the begin of the zip-file name the syntax _temp_username_filename.zip. Why this?

2 – i download the zip, i open it, and i open the txt into this file. When i close it, winrar ask to me if i want to insert new “data”, but i don’t add any data. I just open and close. Why?

I know, its a strange question, but maybe someone had the same problems! Cheers

,

  1. The output filename _temp_username_filename.zip comes from your use of header("Content-Disposition: attachment; filename=$zipstring");. You tried to embed slash / characters, which Firefox does not allow. It turns any / into _. Solution: If you do not like that filename, change $zipstring to something else before sending that header.

  2. That’s Winrar specific behaviour and has little to do with the generated ZIP file. Solution: don’t use Winrar.

Leave a Comment