做了一个php上传下载页面 ,上传文件正常,但下载后就打不开了,说已损坏。。怎么回事
关键是有的机上上可以,有的不行。不知道是哪里的问题。。谢谢好心人帮我解答!!!核心代码:---------------downFile.php--------------...
关键是有的机上上可以,有的不行。不知道是哪里的问题。。谢谢好心人帮我解答!!!
核心代码:
---------------downFile.php----------------
<?php
header("Content-type:text/html; charset=utf-8");
include "downFile.inc.php";
include "include/dbconn.php";
if(empty($_GET['id'])){
return;
}
$id = $_GET['id'];
$sql = "select filename from ".$DBTAG."downfile where id=$id;";
$res = mysql_query($sql,$link) or die(mysql_error());
$record = mysql_num_rows($res);
if($record==0){
echo "<script type='text/javascript'> alert('对不起!该文件不存在或已被删除!'); </script>";
return;
}
$row = mysql_fetch_array($res);
$filename = $row['filename'];
mysql_free_result($res);
downFile($filename);
$sql = "update downfile set downcount=downcount+1 where id=$id;";
mysql_query($sql,$link) or die(mysql_error());
?>
---------------downFile.inc.php----------------
<?php
function downFile($file_name){
$file_name = iconv("utf-8","gb2312",$file_name);
$file_path = "admin/files/".$file_name;
$buffer = 102400; //一次返回102400个字节
if(!file_exists($file_path)){
echo "<script type='text/javascript'> alert('对不起!该文件不存在或已被删除!'); </script>";
return;
}
//若文件存在,则打开文件
$fp = fopen($file_path,"r");
//获取下载文件的大小
$file_size = filesize($file_path);
//返回的文件
header("Content-type:application/octet-stream");
//按照字节大小返回
header("Accept-Ranges:bytes");
//返回文件大小
header("Accept-Length:$file_size");
header("Content-Disposition:attachment; filename=$file_name");
while(!feof($fp)){
$file_data = fread($fp,$buffer);
echo $file_data;
}
fclose($fp);
}
?>
----------------------------------------------------------------------
if(empty($_GET['id'])){
return;
}
这里是表示接受从下载列表页面传过来的 id 。意思是要下载那个文件的id。
------------------------------------------------------------------------- 展开
核心代码:
---------------downFile.php----------------
<?php
header("Content-type:text/html; charset=utf-8");
include "downFile.inc.php";
include "include/dbconn.php";
if(empty($_GET['id'])){
return;
}
$id = $_GET['id'];
$sql = "select filename from ".$DBTAG."downfile where id=$id;";
$res = mysql_query($sql,$link) or die(mysql_error());
$record = mysql_num_rows($res);
if($record==0){
echo "<script type='text/javascript'> alert('对不起!该文件不存在或已被删除!'); </script>";
return;
}
$row = mysql_fetch_array($res);
$filename = $row['filename'];
mysql_free_result($res);
downFile($filename);
$sql = "update downfile set downcount=downcount+1 where id=$id;";
mysql_query($sql,$link) or die(mysql_error());
?>
---------------downFile.inc.php----------------
<?php
function downFile($file_name){
$file_name = iconv("utf-8","gb2312",$file_name);
$file_path = "admin/files/".$file_name;
$buffer = 102400; //一次返回102400个字节
if(!file_exists($file_path)){
echo "<script type='text/javascript'> alert('对不起!该文件不存在或已被删除!'); </script>";
return;
}
//若文件存在,则打开文件
$fp = fopen($file_path,"r");
//获取下载文件的大小
$file_size = filesize($file_path);
//返回的文件
header("Content-type:application/octet-stream");
//按照字节大小返回
header("Accept-Ranges:bytes");
//返回文件大小
header("Accept-Length:$file_size");
header("Content-Disposition:attachment; filename=$file_name");
while(!feof($fp)){
$file_data = fread($fp,$buffer);
echo $file_data;
}
fclose($fp);
}
?>
----------------------------------------------------------------------
if(empty($_GET['id'])){
return;
}
这里是表示接受从下载列表页面传过来的 id 。意思是要下载那个文件的id。
------------------------------------------------------------------------- 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询