将 mysql 搜索结果 url 变量传递给第二个 php 处理器以下载 zip 文件中的首选文件
pass mysql search results url variables into 2nd php processor to download preferred files in a zip file
SITREP
正在 PHP 5.6.11 和 Ubuntu 15.04 上的 MySqli 5.6.30 构建类似购物车的文档 builder/downloader。许多部分单独工作但不能一起工作。
数据库 table 包含产品的具体名称以及相应的文档 url(PDF 存储在服务器上)。
这是事件的顺序,由最终用户的工作流程决定:
- 设置会话cookie。 (TBD 可能隐藏了 iframe)。 PHP 第 1 页。
- 用户通过表单搜索、提交、回显数十个结果+来自数据库的带有复选框的匹配 url。 PHP 第 1 页。
- 用户勾选了几个复选框以找到他们想要下载的项目。 PHP 第 1 页。
- 用户搜索更多,查看更多项目。 PHP 第 1 页。
- 用户点击下载按钮。 PHP 第 1 页。
- PHP 页面 #2 使用数组中 PHP 页面 #1 中的
$results['url']
变量,创建一个 zip,添加已检查的文件并提示下载。
我需要一个解决方案,将 PHP#1 上的数据库搜索结果返回的 urls 变量 $results['url']
传递到 PHP#2 下载处理器数组.
PHP 第 1 页,查询是:
$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>";
这是 PHP 第 2 页处理器的完整代码(来自下面 RajdeepPaul 的解决方案 + 擦除 zip 目录):
$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");
// removes zip file from directory after creation
array_map('unlink', glob("*.pdf"));
array_map('unlink', glob("*.zip"));
我乐于接受并非常感谢任何建议!
按以下方式更改 foreach
循环,
foreach ($files as $file){
$downloaded_file = file_get_contents($file);
$zip->addFromString(basename($file),$downloaded_file);
}
所以您在第 2 页 上的代码应该是这样的:
$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");
SITREP
正在 PHP 5.6.11 和 Ubuntu 15.04 上的 MySqli 5.6.30 构建类似购物车的文档 builder/downloader。许多部分单独工作但不能一起工作。
数据库 table 包含产品的具体名称以及相应的文档 url(PDF 存储在服务器上)。
这是事件的顺序,由最终用户的工作流程决定:
- 设置会话cookie。 (TBD 可能隐藏了 iframe)。 PHP 第 1 页。
- 用户通过表单搜索、提交、回显数十个结果+来自数据库的带有复选框的匹配 url。 PHP 第 1 页。
- 用户勾选了几个复选框以找到他们想要下载的项目。 PHP 第 1 页。
- 用户搜索更多,查看更多项目。 PHP 第 1 页。
- 用户点击下载按钮。 PHP 第 1 页。
- PHP 页面 #2 使用数组中 PHP 页面 #1 中的
$results['url']
变量,创建一个 zip,添加已检查的文件并提示下载。
我需要一个解决方案,将 PHP#1 上的数据库搜索结果返回的 urls 变量 $results['url']
传递到 PHP#2 下载处理器数组.
PHP 第 1 页,查询是:
$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>";
这是 PHP 第 2 页处理器的完整代码(来自下面 RajdeepPaul 的解决方案 + 擦除 zip 目录):
$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");
// removes zip file from directory after creation
array_map('unlink', glob("*.pdf"));
array_map('unlink', glob("*.zip"));
我乐于接受并非常感谢任何建议!
按以下方式更改 foreach
循环,
foreach ($files as $file){
$downloaded_file = file_get_contents($file);
$zip->addFromString(basename($file),$downloaded_file);
}
所以您在第 2 页 上的代码应该是这样的:
$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");