I have a site that allows users to upload files of any kind (jpg, pdf, etc...) and then makes a list for them to download that forces the download rather than view in the browser.
For smaller files it works like a charm, but for larger files the file is truncated at 2,001,000 bytes. The file size is stored correctly in my database.
Here is my download code:
if (is_numeric ($_GET['uid'])) { // Check for an upload ID.
require_once ('../mysql_connect.php'); // Connect to the database.
// Get the information for this file.
$query = "SELECT file_name, file_type, file_size FROM uploads WHERE upload_id = {$_GET['uid']}";
$result = mysql_query ($query);
list ($fn, $ft, $fs) = mysql_fetch_array ($result, MYSQL_NUM);
mysql_close(); // Close the database connection.
// Determine the file name on the server.
$extension = explode ('.', $fn);
$the_file = '../uploads/' . $_GET['uid'] . '.' . $extension[1];
// Check if it exists.
if (file_exists ($the_file)) {
// Send the file.
header ("Content-Type: application/$ft\n");
header ("Content-disposition: attachment; filename=$fn\n");
header ("Content-Length: $fs\n");
readfile ($the_file);
$message = '<p>The file has been sent.</p>';
} else { // File doesn't exist.
$message = '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>';
}
} else { // No valid upload ID.
$message = '<p><font color="red">Please select a valid file to download.</font></p>';
}
The download is called like this:
http://www.mywebsite.com/download_file.php?uid=5
Any suggestions?
Thanks!