3

I'm a real php newbie and i have this code that should allow to create a ziparchive containing the files in the folder where the php file is, give the archive the folder's name, save it in a different folder and download it.

$zipname = getcwd();
$zipname = $zipname.'.zip';
$zip = new ZipArchive;
$zip->open('D:/mypath/zip/'.basename($zipname).'', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
      while (false !== ($entry = readdir($dir_handle))) {
    if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
        $zip->addFile($entry);
    }
  }
  closedir($dir_handle);
}
else {
die('file not found');
}

$zip->close();

So far so good: the zip created has the right name ("foldername.zip") and is exactly where I want it to be, in the /zip/ folder in the root directory of the server. Now I'm stuck with this headers and I can't point the download to the correct location and name of the file

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header("Location: /zip/$zipname");
?>

changing the last header to

header("Location: /zip/foldername.zip");

got it working, but I'm looking for a solution that doesn't force me to edit the php every time i create a new folder.

EDIT: Here's the working code

<?php
$zipname = getcwd();
$zipname = substr($zipname,strrpos($zipname,'\\')+1);
$zipname = $zipname.'.zip';
$zip = new ZipArchive;
$zip->open('D:/inetpub/webs/mydomaincom/zip/'.basename($zipname).'', ZipArchive::CREATE);
if ($dir_handle = opendir('./')) {
      while (false !== ($entry = readdir($dir_handle))) {
    if ($entry != "." && $entry != ".." && !strstr($entry,'.php')) {
        $zip->addFile($entry);
    }
  }
  closedir($dir_handle);
}
else {
die('file not found');
}

$zip->close();

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header('Location: /zip/'.$zipname);
?>
brunogermain
  • 51
  • 1
  • 6
  • Generate a random string and create the subfolder. Move the file there and include the randomly generated string in the final headers. You can use /tmp/ if you only wish the file to be downloaded momentarily and free up space. But you didn't specify that in your case so I'm assuming you want to save the zip archives. – Rimble Oct 30 '15 at 12:26
  • I don't need to keep the zip archives, but i really don't know how to do what you say. I have poor knowledge of coding – brunogermain Oct 30 '15 at 12:40
  • I'm having a bit of trouble understanding what your goal is. Do you want to make a program that converts any given folder into a zip file? As I read it right now the only problem you have is that you have to move and edit the file to convert a folder into a .zip archive. You could turn all of your code into a function and pass along a variable namely the directory you wan't to compress. Then you could use it from anywhere. – Rimble Oct 30 '15 at 12:45
  • my goal is to have each of my clients (i run a recording studio) download mixes, or audio files in general, from their own password protected folder, without clicking each file, grabbing them all at once in a zip archive. index.php generates the html list and download links of the files in that folder – brunogermain Oct 30 '15 at 12:55

2 Answers2

1

Your last header isn't correct :

header("Location: /zip/$zipname");

It should be :

header("Location: /zip/".$zipname);

Since your $zipname is a variable and not a string

Nirnae
  • 1,315
  • 11
  • 23
  • it doesn't work. it points to "www.mydomain.com/zip/:D\mypath\foldername.zip" of course nothing's there... – brunogermain Oct 30 '15 at 12:16
  • Can you dump $zipname ? Pretty sure that you didn't strip_out the path from getcwd() like I told you in your other post – Nirnae Oct 30 '15 at 12:17
  • if I add the strip_out string, the download works fine, but filename is just ".zip" – brunogermain Oct 30 '15 at 12:38
  • My bad, I've done a mistake in my other post on and put a '/' instead of a '\', and also messed up var name, I edited it now, it should work better(since i've tested it this time) : http://stackoverflow.com/questions/33431590/zip-files-in-a-folder-and-give-the-archive-folders-name/33431646#33431646 – Nirnae Oct 30 '15 at 12:42
  • You should switch the resolved mark to my answer otherwhile that might confuse some other people with the same problem ;) – Nirnae Oct 30 '15 at 12:55
  • 1
    @Nirnae This answer doesn't make any sense. A string is a variable. The two things you posted work exactly the same. Double quotations interpolate the variable. – Rimble Oct 30 '15 at 13:24
1

Try

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zipname).'"');
header('Content-Length: ' . filesize($zipname));
header('Location: /zip/'.$zipname);
Salah
  • 139
  • 6