php 檔案下載下來後檔案打不開?解決思路

來源:互聯網
上載者:User
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 '提交有錯誤請重新再試';
}
}
}
}

?>




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');
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.