tags:

views:

12

answers:

1

Hi. 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]=$row[1]." - ".$row[2];

    // 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=$row[0];
    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  A: 
  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.

mario
thanks for the reply man. the problem is that with Chrome (for example), when i send the file (source: /temp/username/filename.zip) i get only the file, called filename.zip. On firefox it add _temp_username before filename.zip. So, how can remove it if the source of that file its on /temp/username? :)
markzzz
Use `$zipstring = "filename.zip";` right before the `header()`.
mario
uhm...but how can work this? it doesnt know how to get the filename.zip. It is into /temp/username folder. in fact if i write $zipstring = $trackid."-GTW-Tracklist.zip"; it doesnt work :(
markzzz
`header("Content-Disposition: attachment; filename=$zipstring");` change it
mario
ah!!! that parameter just put the name of the attachment file. So i add $zipstring2=$trackid."-GTW-Tracklist.zip"; and edited with header("Content-Disposition: attachment; filename=$zipstring2"); Now it works perfectly!!! Thanks for this tip. About winrar behaviour it looks strange, isnt it? If i use gzip for example thats don't happen. But ok, its not my fault i think :)
markzzz