The file cannot be opened after the PHP file is downloaded? Solution Ideas

Source: Internet
Author: User
The file cannot be opened after the PHP file is downloaded?
File Upload code:
$page _title= ' upload file ';
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 + +) {//process each uploaded file.
$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;
}
Add a record to the database
$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 ();
Move uploaded files to a new location
if (Move_uploaded_file ($_files[$filename [' Tmp_name '], "uploads/$upload _id")) {
Echo ' has '. ($i + 1). ' File has been uploaded ';
}else{
Echo ' has '. ($i + 1). ' File upload failed ';
$query = "DELETE from uploads WHERE upload_id= $upload _id";
$result = @mysql_query ($query);
}

}else{//if query no OK.
Echo ' Submit an error please try again ';
}
}
}
}

?>




Include (' Include/footer.inc ');
?>

File download code:

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 being 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 ');
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.