【发布时间】:2015-06-30 11:14:09
【问题描述】:
使用电脑时文件下载正常。 安卓浏览器出现问题。
当我们从 android 默认浏览器下载文件时,该文件已下载但它是 0.00 字节,因此技术上不存在(损坏的文件)。
当我们从任何第三方应用程序(例如或 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上下载文件。
谁能告诉我如何消除错误?这样用户也可以从他们的安卓手机下载文件。
【问题讨论】:
-
stackoverflow.com/questions/4674737/… 查看这个问题,它建议将扩展名放在大写字母中。
-
以大写形式写入 Content-Disposition 文件扩展名,为此我更改了 $ext = strtoupper(ext($name));现在,如果我们下载扩展程序以大写形式出现,但仍然无法在移动设备中下载。如果您想尝试,请从 www.skyshare.in 下载文件
-
请在此处发布下载链接。
-
r or opera it gives error that file could not be downloaded.。你的意思是他们显示这条消息:else header("Location: index.php?msg=Sorry!+File+could+not+bue+downloaded");? -
We give file id to this function。请展示你是如何做到的。您忘记告诉您的脚本名称是downfile.php,并且当单击“下载”按钮之一时,浏览器应该向您的脚本发布file_id值。
标签: php android http-headers