3

I'm creating page where users can upload/download pdf files which is stored in mysql database, the problem that when I download a file, it becomes corrupted/damaged

NP: data of the file stored as blob in the database.

Below is my code:

        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES           ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);

        // Create the SQL query
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);

        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

download code: 
 // Fetch the file information
        $query = "
            SELECT `mime`, `name`, `size`, `data`
            FROM `file`
            WHERE `id` = {$id}";
        $result = $dbLink->query($query);

        if($result) {
            // Make sure the result is valid
            if($result->num_rows == 1) {
            // Get the row
                $row = mysqli_fetch_assoc($result);

                // Print headers
                //header('Content-Type: application/pdf');
                header("Content-Type: application/force-download"); 
                header("Pragma: public"); 
                header("Content-Description: File Transfer");
                header("Content-Type: ".$row['mime']);
                header("Content-Length: ".$row['size']);
                header("Content-Disposition: attachment; filename=".$row['name']);
                header("Content-Transfer-Encoding: binary");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                // Print data
                @readfile($row['data']);
            }
            else {
                echo 'Error! No file exists with that ID.';
            }

            // Free the mysqli resources
            @mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
        }
        @mysqli_close($dbLink);
    }
}
else {
    echo 'Error! No ID was passed.';
}
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
Samer El Gendy
  • 1,683
  • 2
  • 23
  • 45

1 Answers1

2

Not the way. Mysqli is not the way either. You always have a choice, tell your teacher there is no value in this approach. Why not store a jpg in a mysql database? Cause there is a better way - store the link in the database, store the .jpg on web. Same with a PDF. If you must peruse, look at how web based editors like ckedit store data (another wrong approach.), and also look at dompdf code.google.com/p/dompdf/ to get an idea of what breaks the pdf format.

Why is mysqli a bad idea? mySql is what it is. mySql tries to make it something else that is better done with other methods, otherwise mysql would do it.

John Phelps
  • 134
  • 1
  • 7