0
votes

I've php script for creating zip folder of some text files

 <?php
  $path = "path\to\files";
  $files = array($path.'\ionic interaction.txt',$path.'\file1.txt',$path.'\file2.txt',$path.'\file3.txt',$path.'\file3.txt');
  $folder = $_POST['filename'];
  $zipname = $folder.'.zip';
  echo '<form action="download.php" method="post" name="zippass2download"><input    type="hidden" name="zipname" value="'.$zipname.'"></form>';
  $zip = new ZipArchive;
  $zip -> open($zipname, ZipArchive::CREATE);
  foreach($files as $file)
   {
     $zip -> addFile($file);
   }
  $zip -> close();
  ?>

Next to that script I've created a hyperlink to download the zip folder created in the first script.

 <a href="download.php">Download</a>

In download.php I've following codes

<?php
$path2zip = 'C:/wamp/www/PPInt';
$tobezipped = $_POST['zipname'];
header('Content-disposition: attachment; filename=$tobezipped');
header('Content-type: application/zip');
readfile('$path2zip/$tobezipped');
?>

Not getting where is the problem. Can anyone solve this for me?

3
what echo '$variable'; will be? see Thisuser1646111
I don't see anything to submit that <form>Gerald Schneider

3 Answers

1
votes

$tobezipped = $_POST['zipname'];

is not set in the get request that is made when download.php is called. Rember HTTP is stateless so, after you generate the file. POST params don't live in the next GET request when the link is clicked.

You probably need to append the zipname / path to your download.php link as in "download.php?tobezipped=zipname.zip"

1
votes

The "download.php" is not getting "$_POST['zipname']" since you are not posting your form to it instead you are giving link , try to give button for posting the form which have you hidden input for file name. Another thing the code given by "Dasun" below is working just check for relative url for your file and zip file.

0
votes

Try the below code

<?php 
$file_name = $_POST['zipname'];
$file_url = 'C:/wamp/www/PPInt'. $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);
?>