2016-03-01 90 views
2

我需要下載的APK應用程序,但我必須保持我的頁面的用戶或重定向他 另一個網頁的標題,我只能下載應用程序或重定向他到另一個網頁,但我不能同時做兩 我的PHP代碼:下載文件,然後重定向到另一頁

$b= "location"; 



    echo "<script>window.location.href = '$b';</script>" ; 

      $file = 'apk.apk'; 

if (file_exists($file)) { 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
readfile($file); 

} 
+0

HTTP的可能重複:/ /stackoverflow.com/questions/15872534/how-to-download-a-file-then-redirect-to-another-page-in-php – rahul

+0

你能解釋一下我在這個URL給出的解決方案? –

回答

0

@ aron9forever感謝您的幫助,我找到了一個好辦法做到這一點:

$e= "url.com" ; 



echo "<script>setTimeout(function(){window.location.href = '$e';} ,2)</script>" ; 


echo "<iframe src='download.php' ></iframe>" ; 

和的download.php:

<?php 
$file = 'apk1.apk'; 

if (file_exists($file)) { 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
readfile($file); 

} 
?> 
1

根據我的研究(5分鐘谷歌上搜索,你可以做的),你所請求是不可能的。

以一個例子是下載東西,然後一個頁面重定向到「感謝您下載」

實際上他們做的事情是這樣的,他們把你帶到「謝謝」頁面,然後他們開始下載。

要在您的代碼中實現此功能,您需要首先打開$b位置,並在該腳本中通過重定向到包含下載的PHP腳本開始下載。

例子:

的download.php

$file = 'apk.apk'; 

if (file_exists($file)) 
{ 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    readfile($file); 
} 

hello.php

$b= "download.php"; 
echo "Hello! <script>window.location.href = '$b';</script>" ; 

當您訪問hello.php你會看到你好!和下載將開始

+0

我想這樣做,但它下載然後應用程序退出頁面,而不調用javascript代碼 –

+0

@RamiOssmane你說得對,我編輯了答案。 – aron9forever

相關問題