我的服务器中存储了多个文件,可以在我的表中使用用户 ID 和地址找到它们。
它们有不同的文件类型,例如 docx、jpeg 和 pdf。
我有一个 jquery post 方法,它将 user_id 发送到正确的 php 文件,我将在其中运行查询以获取用户文件地址作为数组。
$("#download_btn").click(function () {
var uni_id = $("#uni_selection_filter").val();
$.post("Requests/zipCorsponds.php", //Required URL of the page on server
{ // Data Sending With Request To Server
Get_arch_letters : true,
user_id : vuser_id,
user_role : "<?php echo $_SESSION["role"]; ?>",
section_num : "<?php echo $_SESSION['section_id'] ?>",
uni_id : uni_id
},
function(response){ // Required Callback Function
$("#dynamic_table").html(response);
});
});
现在我在查询后添加了以下方法来压缩这些文件并将结果发送到查看用户能够下载他/她的所有文件的 jquery。
if ($_POST['Get_arch_letters']) {
$user_id = $_POST['user_id'];
$user_role = $_POST['user_role'];
$section_num = $_POST['section_num'];
$uni_id = $_POST['uni_id'];
if ($user_role == 'executive_manager') {
$load_corspnd_letter = $DBM->RunQuery("SELECT at_corspond_file.corspond_file FROM at_corspnd
INNER JOIN at_corspond_file ON (at_corspnd.id = at_corspond_file.corspond_id)
WHERE at_corspnd.uni_id = '$uni_id' ", true, false);
}
$files = mysqli_fetch_assoc($load_corspnd_letter);
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = 'Resume.zip';
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) { die($file.' does not exist'); }
if (!is_readable('../'.$file)) { die($file.' not readable'); }
# download file
$download_file = file_get_contents('../'.$file);
#add it to the zip
$zip->addFile(basename($file),$download_file);
}
# close zip
$zip->close();
echo 'Requests/'.$tmp_file;
}
但我得到的结果是:PK|H�IPK|H�I�PK。
我怎样才能做到这一点?
更新
我添加了条件来检查存档文件是否已创建,它通过了这个条件但我的目录中没有文件:
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = 'Resume.zip';
if($zip->open($tmp_file, \ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true)
echo 'cannot create zip';
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) {
continue;
}
if (!is_readable('../'.$file)) { die($file.' not readable'); }
#add it to the zip
$zip->addFile(basename($file));
}
# close zip
$zip->close();
echo 'Requests/'.$tmp_file;
最佳答案
您无法使用您的方法下载文件。现在您的代码试图显示您调用服务器的结果。 您有 2 个选项来完成这项工作。
<强>1。使用您当前的 php 代码
如果直接在浏览器中调用,php 代码就可以工作。您可以使用 '#download_btn' 提交具有正确输入值的表单
HTML
<form action="Requests/zipCorsponds.php" method="post" id="frm">
<input type="hidden" name="Get_arch_letters" value="true" />
<input type="hidden" name="user_role" value="<?php echo $_SESSION["role"]; ?>" />
...
<input type="hidden" name="uni_id" id="uni_id" value="" />
</form>
jquery
$("#download_btn").click(function () {
var uni_id = $("#uni_selection_filter").val();
$("#uni_id").val(uni_id);
$("#frm").submit();
});
.
<强>2。使用您当前的 jquery post 方法
您需要更改回调函数以下载返回的文件。
jquery
...
function(response){ // Required Callback Function
window.location = response;
});
...
并更改 php 代码以返回文件路径而不是文件本身。您还需要使用正确的扩展名正确命名文件。
php
...
# create a file & open it
$tmp_file = 'Resumes.zip';
$zip->open($tmp_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
# loop through each file
foreach($files as $file){
if (!file_exists('../'.$file)) { die($file.' does not exist'); }
if (!is_readable('../'.$file)) { die($file.' not readable'); }
#add file to the zip
$zip->addFile(basename('../'.$file));
}
# close zip
$zip->close();
# send the file to the post request
echo 'Requests/'.$tmp_file;
强>强>关于php - 压缩多个文件,其地址存储在 mysql 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255985/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何