php 文件下载下来后文件打不开?

knifeguest 2011-07-07 09:49:02
文件上传代码:
<?php #上传文件
$page_title='上传文件';
include('include/header.inc');
$counter=3; //Number files of allow upload.
if(isset($_POST['submitted'])){ //hidden form
require_once ('/conn.php'); // Connect to the database.
for($i=0;$i<$counter;$i++){ //处理每一个上传的文件.
$filename='upload'.$i; //
$description='description'.$i;
//check for file.
if(isset($_FILES[$filename])&&($_FILES[$filename]['error'] !=4)){
//check discription
if(!empty($_POST[$description])){
$d=trim($_POST[$description]);
}else{
$d=NULL;
}
//添加一条记录到数据库
$query="INSERT INTO uploads (file_name,file_size,file_type,description) VALUES ('{$_FILES[$filename]['name']}','{$_FILES[$filename]['size']}',
'{$_FILES[$filename]['type']}','$d') ";
$result=@mysql_query($query);
if($result){
$upload_id=mysql_insert_id();
// 将上传的文件移动到新位置
if(move_uploaded_file($_FILES[$filename]['tmp_name'],"uploads/$upload_id")){
echo '有'.($i+1).'条文件已经上传';
}else{
echo '有'.($i+1).'条文件上传失败';
$query="DELETE FROM uploads WHERE upload_id=$upload_id";
$result=@mysql_query($query);
}

}else{ //if query no ok.
echo '提交有错误请重新再试';
}
}
}
}

?>
<!--display form -->
<div class="con_message">
<form enctype="multipart/form-data" action="upload.php" method="post">
<fieldset><legend>Fill out the form to upload a file:</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
echo '<p><b>File:</b> <input type="file" name="upload' . $i . '" /></p>
<p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br />
';
}
?>
</fieldest>
<p><input type="hidden" name="submitted" value="TURE" /></p>
<p><input type="submit" value="submit" name="submit" /></p>
</form>
</div>
<?php
include('include/footer.inc');
?>

文件下载代码:
<?php # download_file.php

// Check for an upload_id.
if (isset($_GET['uid'])) {
$uid = (int) $_GET['uid'];
} else { // Big problem!
$uid = 0;
}

if ($uid > 0) { // Do not proceed!

require_once ('./conn.php'); // Connect to the database.

// Get the information for this file.
$query = "SELECT file_name, file_type, file_size FROM uploads WHERE upload_id=$uid";
$result = mysql_query ($query);
list ($fn, $ft, $fs) = mysql_fetch_array ($result);

$the_file = 'uploads/' . $uid;

}
// Check if it exists.
if (file_exists ($the_file)) {

// Send the file.
header ("Content-Type: $ft");
header ("Content-disposition: attachment; filename=\"$fn\"");
header ("Content-Length: $fs");
readfile ($the_file);

} else { // File doesn't exist.
$page_title = 'File Download';
include ('./include/header.inc');
echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>';
include ('./include/footer.inc');
}

} else { // No valid upload ID.
$page_title = 'File Download';
include ('./include/header.inc');
echo '<p><font color="red">Please select a valid file to download.</font></p>';
include ('./include/footer.inc');
}

?>

比如说上传了图片文件,下载下来后打不开,如果是文档文件,下载下来打开是乱码 这是问什么? 这些代码是从php与mysql基础教程上来的
...全文
511 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwcang 2011-07-17
  • 打赏
  • 举报
回复
确实如此
ohmygirl 2011-07-08
  • 打赏
  • 举报
回复
把你下载的文件与原始文件用支持二进制的编辑器打开,看看有什么区别。图片下载下来不能看肯定是多了某些字节,向楼上说的,最好指明二进制下载,header("Content-Transfer-Encoding:binary");
乱码的问题应该是编码的原因。把你的php文件用UTF-8 无bom格式保存试试.
小静静 2011-07-07
  • 打赏
  • 举报
回复
一般都是编码的问题,上传最好使用二进制啊
knifeguest 2011-07-07
  • 打赏
  • 举报
回复
回楼上的 我试了,不行
xuzuning 2011-07-07
  • 打赏
  • 举报
回复
关于下载
1、你的 download_file.php、conn.php 均不可在php标记之外有任何东西,包括BOM头
2、发送的头中最好有二进制声明 header("Content-Transfer-Encoding:binary");

knifeguest 2011-07-07
  • 打赏
  • 举报
回复
图片下载下来后 我看了下图片的属性 没有尺寸,宽带,高度,位深度
piaolankeke 2011-07-07
  • 打赏
  • 举报
回复
文件和数据库编码都一致吗?

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧