2016-06-19 36 views
0

我使用PHP根據用戶提供的輸入在apache服務器上編譯應用程序,然後使用readfile()方法嚮應用程序提供應用程序。
我面臨的問題是,實際下載應用程序時,.apk文件正在以文本文件的形式打開。
enter image description here
使用PHP從服務器下載apk時的垃圾輸出

我已經在PHP代碼中添加了Android應用程序MIME類型。
我的PHP代碼:

<?php 
echo "Please wait while the app is being generated, it can take around 10 minutes for the build to finish.The completed app will be mailed to you "; 

if(isset($_POST['timestamp'])) 
{ 
$uid = $_POST['timestamp']; 
exec("sudo python /var/www/html/appgenserver.py $uid"); 

header("Content-Description: File Transfer"); 
header("Content-Type: application/vnd.android.package-archive"); 
header("Content-Transfer-Encoding: Binary");  //Adding or removing this hass no effect 
header('Content-Disposition: attachment; filename="Foo.apk"'); 
ob_clean(); 
flush(); 
readfile("/var/www/html/".$uid."/releaseapk.apk"); 
exit(); 
} 
?> 

任何幫助,將不勝感激。

回答

1

說得那麼我所做的,而不是返回來自PHP腳本的應用程序,我將它返回給AJAX調用的URL。

從那裏我導航到該URL並自動啓動下載。

PHP代碼:

<?php 
{ 
    echo "http://server_ip/release/".$uid."/releaseapk.apk"; 
} 
?> 

阿賈克斯CAL:

$.ajax({ 
     type: "POST", 
    url: "/test.php", 
    data: { timestamp : timestamp }, 
     success: function(response){ 
      console.log("Success",response); 
      window.location = response; 
    } 
     }); 
1

我認爲你可以通過消除回聲來解決它。在發送標題之前不要輸出到頁面。

+0

註釋掉echo語句了。但仍然是同樣的錯誤。 –

+1

我會從這個頁面上的一個例子開始http://php.net/manual/en/function.readfile.php並讓它起作用,然後每次添加一個附加的東西。 –

+0

謝謝, 但我通過其他方法修復了它,並且已經回答了問題 –

1

造成緩衝區中的亂碼輸出不乾淨,搜索頁面上的輸出頭前或使用ob_end_clean()代替ob_clean()嘗試之前你header()

+0

嘿, 這樣做會停止垃圾代碼的輸出,但下載不會啓動。 –