0

I have some pdf files in folder 3_day_notice_fad_pdf. Now I have to download pdf files in zip form, based on query condition.

<?php
 include("connection.php");
extract($_REQUEST);
$query=mysql_query("select fad_html_name from fad_record where t_id='$word'") or die(mysql_error());
while($result=mysql_fetch_array($query,MYSQL_ASSOC))
{
extract($result);   
$movies_id[] = "3_day_notice_fad_pdf/$fad_html_name";
}
 $movies_id = array();

 //print_r($movies_id);
  $zipname = 'file.zip';
  $zip = new ZipArchive;
   $zip->open($zipname, ZipArchive::CREATE);
  foreach ($movies_id as $file) {
   $zip->addFile($file);
  }
  $zip->close();
  header('Content-Type: application/zip');
  header('Content-disposition: attachment; filename='.$zipname);
  header('Content-Length: ' . filesize($zipname));
  readfile($zipname);
  ?>

Zip file is working perfectly, but it is downloading all the files present in folder 3_day_notice_fad_pdf. It is not validating with condition where t_id='$word' in query.

Naeem Ul Wahhab
  • 2,465
  • 4
  • 32
  • 59
user3526766
  • 93
  • 1
  • 3
  • 16

1 Answers1

1

You are doing several bad things here.

  1. Do not pass query strings directly to SQL. It will lead to SQL injection and your application will be compromised. See here: https://stackoverflow.com/a/60496/2520628

  2. You are clearing your files array just after loop

Corrected code:

<?php
 include("connection.php");
$word = $_REQUEST['word'];
$query=mysql_query("select fad_html_name from fad_record where t_id='$word'") or die(mysql_error());
while($result=mysql_fetch_array($query,MYSQL_ASSOC))
{ 
$movies_id[] = "3_day_notice_fad_pdf/" . $result['fad_html_name'];
}

  $zipname = 'file.zip';
  $zip = new ZipArchive;
  $zip->open($zipname, ZipArchive::CREATE);
  foreach ($movies_id as $file) {
    $zip->addFile($file);
  }
  $zip->close();
  header('Content-Type: application/zip');
  header('Content-disposition: attachment; filename='.$zipname);
  header('Content-Length: ' . filesize($zipname));
  readfile($zipname);
  ?>
Tejashwi Kalp Taru
  • 2,994
  • 2
  • 20
  • 35