我有一个页面,用户点击" DOWNLOAD"按钮。 这将打开一个模式,需要CODE来授权下载。
插入后,CODE将被提交给PHP脚本,该脚本验证代码并恢复文件OUTSIDE webroot并需要强制下载。
但是,我在Firebug中看到脚本"回声"文件内容......
我需要"关闭"模式和强制下载。非常感谢你!
HTML(模态)
<div class="modal fade" id="downloadpayroll" tabindex="-1" role="dialog" aria-labelledby="downloadpayrolllabel">
<div class="modal-dialog" role="document">
<div class="modal-content" id="modal-form-container">
<form method="post" class="form" id="enable-payroll-download" action="{{ @ABSOLUTE_PATH }}{{ @ALIASES.url_payroll_new }}">
<input type="hidden" name="payroll_id" value="" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Conferma download</h4>
</div>
<div class="modal-body">
<p>Per confermare il download, <strong>inserisci il tuo PIN</strong></p>
<div class="form-group">
<label for="pin">PIN</label>
<input type="text" class="form-control input-lg text-center required-field numbers-only" name="pin" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
<button type="submit" class="btn btn-primary">Scarica cedolino</button>
</div>
</form>
</div>
</div>
</div>
JQUERY提交
var data = $form.serialize();
data = data+'&ajax=true';
$.ajax({
data : data,
type : $form.attr('method'),
url : $form.attr('action'),
success : function(response) {
// nothing to do here
} // success
});
PHP脚本
// other stuffssss
if ($validate===true) {
// IL PIN E' CORRETTO, TIRIAMO FUORI IL CEDOLINO
$payroll = $this->service->getPayroll($id_payroll,'id',null,'ASC',1);
$path = \SupportText::removeSubstringFromString($this->f3->get('SERVER.DOCUMENT_ROOT'), '/httpdocs').'/private';
$filename = $path.'/'.$this->id_user.'/'.$payroll[0]->filename;
\SupportFile::returnFile($filename);
exit;
}
PHP SupportFile类
class SupportFile {
public static function returnFile( $filename ) {
// Check if file exists, if it is not here return false:
if ( !file_exists( $filename )) return false;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
// Suggest better filename for browser to use when saving file:
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
// Caching headers:
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// This should be set:
header('Content-Length: ' . filesize($filename));
// Clean output buffer without sending it, alternatively you can do ob_end_clean(); to also turn off buffering.
ob_clean();
// And flush buffers, don't know actually why but php manual seems recommending it:
flush();
// Read file and output it's contents:
readfile( $filename );
// You need to exit after that or at least make sure that anything other is not echoed out:
exit;
}
}
答案 0 :(得分:0)
尝试使用ob_start();在header()之前。然后使用下面的代码
class SupportFile {
public static function returnFile( $filename ) {
if ( !file_exists( $filename )) return false;
ob_strat();
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($filename));
header("Content-Length: " . filesize($filename));
header('Content-Type: '.mime_content_type($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_end_clean();
ob_clean();
flush();
readfile( $filename );
exit;
}
}