php 文件下载下来后文件打不开?解决思路
程序员文章站
2024-01-29 10:33:10
...
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 $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') ";
[email protected]_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";
[email protected]_query($query);
}
}else{ //if query no ok.
echo '提交有错误请重新再试';
}
}
}
}
?>
include('include/footer.inc');
?>
文件下载代码:
// 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 '
include ('./include/footer.inc');
}
} else { // No valid upload ID.
$page_title = 'File Download';
include ('./include/header.inc');
文件上传代码:
$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 $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') ";
[email protected]_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";
[email protected]_query($query);
}
}else{ //if query no ok.
echo '提交有错误请重新再试';
}
}
}
}
?>
include('include/footer.inc');
?>
文件下载代码:
// 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 '
The file could not be located on the server. We apologize for any inconvenience.
';include ('./include/footer.inc');
}
} else { // No valid upload ID.
$page_title = 'File Download';
include ('./include/header.inc');
相关文章
相关视频