简体   繁体   中英

Corrupt file downloads from MySQL database using PHP

Every time I try to download, it produces correct filename and extension (file) but corrupt (usually 1kb in size and can't be opened, in this case my file is .docx format and around 12kb in size). I'm using XAMPP, and I use phpmyadmin to look at my sql database. The upload script works to store file from computer to database.

Here is my upload script: - edited according to answers

<?php
session_start();
include("connect.php");
$nametemp=$_SESSION['name'];
$nistemp=$_SESSION['nislogin'];
$subjecttemp=$_SESSION['subject'];
$chapter=$_POST['chapter'];

$name=$_FILES['browsefile']['name'];
$mime=$_FILES['browsefile']['type'];
$data=file_get_contents($_FILES['browsefile']['tmp_name']);
$size=$_FILES['browsefile']['size'];

mysql_query("INSERT INTO assignment (name, mime, size, data, created, uploader, subject, chapter) VALUES ('$name', '$mime', '$size', '$data', NOW(), '$nametemp', '$subjecttemp', '$chapter')");

header("location:subject.php");
die();
?>

and here's download script: -edited according to answers

<?php
session_start();
include("connect.php");
$nametemp=$_SESSION['name'];
$nistemp=$_SESSION['nislogin'];
$classtemp=$_SESSION['class'];
$subjecttemp=$_SESSION['subject'];
$idtemp=$_GET['id'];

$query=("SELECT mime, name, size, data FROM assignment WHERE id='$idtemp'");
$result=mysql_query($query);
$filename=mysql_result($result,0,"name");
$filesize=mysql_result($result,0,"size");
$filemime=$type=mysql_result($result,0,"mime");
$filedata=mysql_result($result,0,"data");

header("Content-disposition: attachment; filename=$filename");
header("Content-length: $filesize");
header("Content-type: $filemime");
header("Content-transfer-encoding: binary");
echo $filedata;
?>

In case you're wondering, here is the screenshot of 'assignment' table structure: image

$data=$_FILES['browsefile']['tmp_name'];

You assign the temp name of an uploaded file to the 'data' column, not the data itself. The quick-fix for this is to use file_get_contents function.

Second: readfile reads a file from the disk, it does not provide an output stream from a variable. This line is not needed.

But: saving a binary file in a database is not recommended. Beter to store the file itself in an upload folder and save the name of it.

Also, change the next line

mysql_query("INSERT INTO assignment (name, mime, size, data, created, uploader, subject, chapter) VALUES ('$name', '$mime', '$size', '$data', NOW(), '$nametemp', '$subjecttemp', '$chapter')");

into

mysql_query("INSERT INTO assignment (name, mime, size, data, created, uploader, subject, chapter) VALUES ('$name', '$mime', '$size', '" . mysql_real_escape_string($data) . "', NOW(), '$nametemp', '$subjecttemp', '$chapter')");

Next to this: please consider changing to PDO instead of using the old Mysql_* functions (they're deprecated).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM