0

PHPを使用したファイルダウンロードコードがあり、ダウンロードページのコードは次のとおりです。

 if (file_exists($strDownload)) {

    //get the file content
     $strFile = file_get_contents($strDownload);

       //set the headers to force a download
      header("Content-type: application/force-download");
     header("Content-Disposition: attachment; filename=\"" . str_replace(" ", "_", $arrCheck['file_name']) . "\"");

      //echo the file to the user
     echo $strFile;

     //update the DB to say this file has been downloaded
       mysql_query("xxxxxxxx");

           exit;
     }

関数file_exists()が有効なチェックで渡された場所と私の変数は、サーバーフォルダーにある$strDownloadようなものになります。/home/public_html/uploads/myfile.zipしかし、ファイルをダウンロードする代わりにダウンロードしようとすると、ページにはファイルの完全に暗号化されたソースが表示されます。ダウンロードできるようにするにはどうすればよいですか?

編集:情報については、私はワードプレスシステム内でこのコードを使用しようとしていますが、私のファイルパスはhttp://example.com/wp-content/uploads/2016/02/myfile.zip. また、上記のコードfile_exists()では、すでに上で述べたサーバーパスの条件を自分でチェックし、1必要に応じて返します。

4

2 に答える 2

0

これを試して

        if (file_exists($file)) 
        {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            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));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
于 2016-02-02T08:14:31.967 に答える