Harish Pareek
Harish Pareek

Reputation: 120

how to provide download link for already zipped files in php

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?

Upvotes: 0

Views: 2364

Answers (3)

Dharmendra
Dharmendra

Reputation: 216

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.

Upvotes: 1

Techie
Techie

Reputation: 45124

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);
?>

Upvotes: 0

j_mcnally
j_mcnally

Reputation: 6958

$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"

Upvotes: 1

Related Questions