无法在安卓浏览器中使用php下载文件
问题描述:
使用计算机文件时正常下载。 在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手机下载文件。
答
我得到了解决。 主要是我改变了两件事:
- 使用GET方法,而不是Post方法。
- 使用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浏览器下载文件了。
希望这可能有所帮助。 :)
答
可能$ 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);
http://stackoverflow.com/questions/4674737/avoiding-content-type-issues-when-downloading-a-file-via-browser-on-android检查出这个问题,它建议把扩展名在首都。 – STLMikey
写入内容处理文件扩展名为UPPERCASE 为此,我更改$ ext = strtoupper(ext($ name)); 现在,如果我们下载的扩展名大写,但仍然无法在手机中下载。 如果你想试试,请从www.skyshare.in下载一个文件 – Paras
请在这里下载链接。 – greenapps