Raphelo Raphelo - 5 months ago 11
SQL Question

pass mysql search results url variables into 2nd php processor to download preferred files in a zip file

SITREP



In the middle of building a cart-like document builder/downloader in PHP 5.6.11 and MySqli 5.6.30 on Ubuntu 15.04. A lot of parts work individually but not together.

A database table contains the specific names of products along with corresponding documentation urls (where the PDF's are stored on the server).

Here's the sequence of events, dictated by the end-user's workflow:


  1. Set session cookie. (TBD probably hidden iframe). PHP Page #1.

  2. User searches via form, submits, echos dozens of results + matching url's from the database with checkboxes. PHP Page #1.

  3. User checks a few boxes for items they want to download. PHP Page #1 .

  4. User searches more, check some more items. PHP Page #1.

  5. User hits download button. PHP Page #1.

  6. PHP Page #2 uses the
    $results['url']
    variables from PHP Page #1 in an array, creates a zip, adds files that were checked and prompts download.



I need a solution to pass the returned urls variables
$results['url']
from the database search results on PHP #1 into the PHP#2 download processor array.



PHP page #1, query is:

$raw_results = mysql_query("SELECT * FROM mobilesearchspec
WHERE (`url` LIKE '%".$query."%') OR (`product` LIKE '%".$query."%') ORDER BY product ") or die(mysql_error());

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysql_fetch_array($raw_results)){

echo "<p><input type=checkbox name=item[] value=$results[url]>" ; echo "".$results['product']."<br>";
echo "<a href='" .$results['url'] . "'>". $results['url'] . "</a>";


Here's the full code of PHP Page #2 processor (from RajdeepPaul's solution below):

$files = $_POST['item'];

$timestamp = date("M-d-Y_H:i:s"); //$timestamp takes the current time
$zipname = "zip_".$timestamp.".zip"; // add timestamp to the file name
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file){
$downloaded_file = file_get_contents($file);
$zip->addFromString(basename($file),$downloaded_file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile("$zipname");


I am open to and extremely grateful for any suggestions!

Answer

Change your foreach loop in the following way,

foreach ($files as $file){
    $downloaded_file = file_get_contents($file);
    $zip->addFromString(basename($file),$downloaded_file);
}

So your code on Page #2 should be like this:

$files = $_POST['item'];

$timestamp = date("M-d-Y_H:i:s"); //$timestamp takes the current time
$zipname = "zip_".$timestamp.".zip"; // add timestamp to the file name
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file){
    $downloaded_file = file_get_contents($file);
    $zip->addFromString(basename($file),$downloaded_file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile("$zipname");