1

私たちはウェブシットを開発しています。ボタンがあります。ボタン アクションでは、PDF ファイルのダウンロードを配置しました。正常に動作しています。しかし、Web サイトから PDF ファイルをダウンロードするにはどうすればよいですか。

このようにしてみましたが、得られません。

<?php
//$Down=$_GET['Down'];
$a="hello";
$i=0;
?>
<script type="text/javascript">
    var i=0;
    function submitForm()
    {
        <?php
        $i = @file_get_contents('count.txt'); // read the hit count from file
        echo $i; //  display the hit count
        $i++; // increment the hit count by 1
        @file_put_contents('count.txt', $i); // store the new hit count
        ?>  
    }
</script>
<html>
<head>
</head>

<body>
    <input type="button" onclick="submitForm()" value="submit 1" />
</body>
</html>

何もありません。初心者PHPです。ご案内ください。

4

2 に答える 2

4

PDF をダウンロードして、フォームの送信時にカウントできます。

<?php
$i=0;
$i = (int) @file_get_contents('count.txt'); // read the hit count from file
if(!empty($_POST)) {
    $i++; // increment the hit count by 1
    @file_put_contents('count.txt', $i); // store the new hit count
    // Download
    $file = 'filename.pdf';
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
}

ここで、ダウンロード数を表示する場所を使用echo $i;します。

html では、フォームは次のようになります。

<form method="post" action="">
    <input type="submit" name="download" value="Download">
</form>
于 2015-04-22T14:56:14.717 に答える