0
<?php
include "function.php";

$account = findSomeFile();
$file_url = '/var/www/html/tvconfig/netflix/'.$account.'.zip';
echo $account;

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);

?>

上記は私のPHPスクリプトです。ページをロードすると、ブラウザーにダウンロード プロンプトが表示されますが、ページにエコーはありません。readfile 関数の後にエコーを配置しようとしましたが、機能しません。

4

1 に答える 1

2

ちょっとしたトリックをすることができます:

<?php
$account = "38950596";
$file_url = "$account.json";
echo "Hello Friend!\n";

$fc = file_get_contents($file_url);
?>
<script type="text/javascript">
window.onload = function(e) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('<?php echo $fc ?>'));
    pom.setAttribute('download', '<?php echo $account ?>');
    pom.click();
}
</script>

これによりアンカー要素が作成され、ページが読み込まれるとすぐに、クリック イベントを使用してファイルのダウンロードが強制されます。

デモ: http://main.xfiddle.com/e1a35f80/38950596_stackoverflow.php

于 2016-08-15T07:30:38.007 に答える