2015-04-22 225 views
0

使用計算機文件時正常下載。 在Android瀏覽器上啓動問題。無法在安卓瀏覽器中使用php下載文件

當我們從android默認瀏覽器下載一個文件時,該文件被下載,但是它是0.00字節,所以沒有技術上存在(損壞的文件)。

當我們從任何第三方應用程序如opera或opera下載文件時,會出現「文件無法下載」的錯誤。不是頭錯誤。並且可以通過UC瀏覽器,Chrome和Firefox下載文件。但仍然在默認瀏覽器中給出錯誤。

這裏是我使用的代碼:

<?php 
require 'php/db.php'; 
if(isset($_POST['file_id'])&&!empty($_POST['file_id'])){ 
    download_file($_POST['file_id']); 
} 
function download_file($id){ 
    global $con; 
    $id = mysqli_real_escape_string($con,htmlentities($id)); 
    $file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id"; 
    $result = mysqli_query($con,$file); 
    $row = mysqli_fetch_assoc($result); 
    $name = $row['file_name']; 
    $title = $row['file_title']; 
    $size = $row['file_size']; 
    $ext = strtoupper(ext($name)); // Function defined bellow 
    $down = $row['down']; 
    $newname = $title.'.'.$ext; 
    $olddir = "files/".$name; 
    $down++; 

    if(is_file($olddir)) { 
     $update_down = "UPDATE files SET down = $down WHERE file_id = '$id'"; 
     $update_down_result = mysqli_query($con,$update_down); 

     header('Pragma: public'); // required 
     header('Expires: 0');  // no cache 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($olddir)).' GMT'); 
     header('Cache-Control: private',false); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename="'.$newname.'"'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Content-Length: '.$size); // provide file size 
     header('Connection: close'); 
     readfile($olddir); 

     exit(); 
    }else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded"); 

} 

function ext($name){ 
    $rut = strrev($name); 
    $erut = explode('.', $rut); 
    return strrev($erut[0]); 
} 

我們給文件ID給這個函數,並下載PC上的文件。

任何人都可以告訴我如何刪除錯誤?因此,用戶也可以從他們的Android手機下載文件。

+0

http://stackoverflow.com/questions/4674737/avoiding-content-type-issues-when-downloading-a-file-via-browser-on-android檢查出這個問題,它建議把擴展名在首都。 – STLMikey

+0

寫入內容處理文件擴展名爲UPPERCASE 爲此,我更改$ ext = strtoupper(ext($ name)); 現在,如果我們下載的擴展名大寫,但仍然無法在手機中下載。 如果你想試試,請從www.skyshare.in下載一個文件 – Paras

+0

請在這裏下載鏈接。 – greenapps

回答

0

我得到了解決。 主要是我改變了兩件事:

  1. 使用GET方法,而不是Post方法。
  2. 使用Flush和ob_clean函數清除緩衝區。

爲downfile.php新的代碼是這樣的:

<?php 
    require '../php/db.php'; 
    ob_start(); 
    if(isset($_GET['file_id'])&&!empty($_GET['file_id'])){ 
     download_file($_GET['file_id']); 
    }else die("There was an error in downloading file. Please try again later."); 

    function download_file($id){ 
     global $con; 
     $id = mysqli_real_escape_string($con,htmlentities($id)); 

     $file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id"; 
     $result = mysqli_query($con,$file); 
     $row = mysqli_fetch_assoc($result); 
     $name = $row['file_name']; 
     $title = $row['file_title']; 
     $ext = ext($name); 
     $down = $row['down']; 
     $newname = $title.'.'.$ext; 
     $size = $row['file_size']; 
     $down++; 

     if(is_file($name)) { 
      $update_down = "UPDATE files SET down = $down WHERE file_id = '$id'"; 
      $update_down_result = mysqli_query($con,$update_down); 

      header('Content-Description: File Transfer'); 
      header('Content-Type: application/octet-stream'); 
      header('Content-Disposition: attachment; filename="'.$newname.'"'); 
      header('Content-Transfer-Encoding: binary'); 
      header('Expires: 0'); 
      header('Cache-Control: must-revalidate'); 
      header('Pragma: public'); 
      header('Content-Length: '.$size); 
      ob_clean(); 
      flush(); 
      readfile($name); 
      exit; 

     }else header("Location: index.php?msg=Sorry!+File+could+not+found!"); 

    } 

    function ext($name){ 
     $rut = strrev($name); 
     $erut = explode('.', $rut); 
     return strrev($erut[0]); 
    } 

?> 

通過這個代碼,我能夠在Android瀏覽器下​​載文件了。

希望這可能有所幫助。 :)

0

可能$ size == 0;

$size = $row['file_size']; 
... 
$olddir = "files/".$name; 

變化

$olddir = "files/".$name; 
$size = filesize($olddir); 

,並更改

else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded"); 

else header("Location: index.php?msg=Sorry!+File+could+not+be+found+on+server: " . $olddir); 
+0

仍然無法正常工作。 我按照你的建議改變了我的代碼。但不是。還有同樣的問題。 – Paras

+0

您的代碼與我建議的更改完美地運行在我的服務器上。但我使用GET請求。你也可以試試這個。 – greenapps

+0

您可以通過添加'if(isset($ _GET ['file_id'])&&!empty($ _GET ['file_id']))來爲POST和GET準備您的腳本{ download_file($ _ GET ['file_id' ]);' – greenapps