iarp Posted July 22, 2008 Share Posted July 22, 2008 <?php # Script 12.10 - download_file.php // This pages handles file downloads through headers. // Check for an upload_id. if (isset($_GET['uid'])) { $uid = (int) $_GET['uid']; } else { // Big problem! $uid = 0; } require_once ('./includes/mysql_connect.php'); // Connect to the database. if ($uid > 0) { // Do not proceed! // Get the information for this file. $query = "SELECT file_name, file_type, file_size FROM " . DB_UPLOADS ." WHERE upload_id=$uid"; $result = mysql_query ($query); list ($fn, $ft, $fs) = mysql_fetch_array ($result, MYSQL_NUM); // Determine the file name on the server. $the_file = './files/' . $uid; // Check if it exists. if (file_exists ($the_file)) { // Send the file. header ("Content-Type: $ft\n"); header('Content-Disposition: attachment; filename="' . $ft . '"\n'); header ("Content-Length: $fs\n"); readfile ($the_file); exit(); } else { // File doesn't exist. $page_title = 'File Download'; include ('./includes/header.php'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('./includes/footer.php'); } } include ('./includes/header.php'); $first = TRUE; // Initialize the variable. // Query the database. $query = "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM " . DB_UPLOADS . " ORDER BY date_entered DESC"; $result = mysql_query ($query); // Display all the URLs. while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee'); // If this is the first record, create the table header. if ($first) { echo '<div style="text-align: center;"><small>To use the links below, right click the File Name, Copy link location.<br />When dowloading files, you must SAVE the file with it\'s proper name.</small></div>'; echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="20%"><font size="+1">File Name</font></td> <td align="left" width="40%"><font size="+1">Description</font></td> <td align="center" width="20%"><font size="+1">File Size</font></td> <td align="left" width="20%"><font size="+1">Upload Date</font></td> </tr>'; $first = FALSE; // One record has been returned. } // End of $first IF. // Display each record. echo " <tr bgcolor=\"' . $bg . '\"> <td align=\"left\"><a href=\"./download.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td> <td align=\"left\">" . stripslashes($row['description']) . "</td> <td align=\"center\">{$row['fs']}kb</td> <td align=\"left\">{$row['d']}</td> </tr>\n"; } // End of while loop. // If no records were displayed... if ($first) { echo '<div align="center">There are currently no files to be viewed.</div>'; } else { echo '</table>'; // Close the table. } include ('./includes/footer.php'); ?> // Send the file. header ("Content-Type: $ft\n"); header('Content-Disposition: attachment; filename="' . $fn . '"\n'); header ("Content-Length: $fs\n"); readfile ($the_file); exit(); This is where i'm having trouble. filename="' . $fn . '"\n'); doesn't output the files name properly. When you go to download an item, it shows up as download.php (the pages name) Something i'm doing wrong? test script: www.moha.iarp.ca/download.php EDIT: forgot to add, if i hard code a file's name into filename="" it shows up as w/e i set it to. Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/ Share on other sites More sharing options...
DarkWater Posted July 22, 2008 Share Posted July 22, 2008 Is it in the DB properly? Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596675 Share on other sites More sharing options...
iarp Posted July 22, 2008 Author Share Posted July 22, 2008 Well on the test page, it pulls that info from the db... heres the info anyways CREATE TABLE `MOHA_uploads` ( `upload_id` int(10) unsigned NOT NULL auto_increment, `file_name` varchar(30) NOT NULL default '', `file_size` int(6) unsigned NOT NULL default '0', `file_type` varchar(30) NOT NULL default '', `description` varchar(100) default NULL, `date_entered` timestamp(14) NOT NULL, PRIMARY KEY (`upload_id`), KEY `file_name` (`file_name`), KEY `date_entered` (`date_entered`) ) TYPE=MyISAM AUTO_INCREMENT=5 ; -- -- Dumping data for table `MOHA_uploads` -- INSERT INTO `MOHA_uploads` VALUES (1, 'MOHABylaw.pdf', 73431, 'application/pdf', 'MOHA', '20080521021044'); Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596677 Share on other sites More sharing options...
DarkWater Posted July 22, 2008 Share Posted July 22, 2008 Ahem...you have: header('Content-Disposition: attachment; filename="' . $ft . '"\n'); Instead of: header('Content-Disposition: attachment; filename="' . $fn . '"\n'); >_> Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596681 Share on other sites More sharing options...
iarp Posted July 22, 2008 Author Share Posted July 22, 2008 oops.... that was from me screwing around with it before posting here. <?php # Script 12.10 - download_file.php // This pages handles file downloads through headers. // Check for an upload_id. if (isset($_GET['uid'])) { $uid = (int) $_GET['uid']; } else { // Big problem! $uid = 0; } require_once ('./includes/mysql_connect.php'); // Connect to the database. if ($uid > 0) { // Do not proceed! // Get the information for this file. $query = "SELECT file_name, file_type, file_size FROM " . DB_UPLOADS ." WHERE upload_id=$uid"; $result = mysql_query ($query); list ($fn, $ft, $fs) = mysql_fetch_array ($result, MYSQL_NUM); // Determine the file name on the server. $the_file = './files/' . $uid; // Check if it exists. if (file_exists ($the_file)) { // Send the file. header ("Content-Type: $ft\n"); header ("Content-disposition: attachment; filename=\"$fn\"\n"); header ("Content-Length: $fs\n"); readfile ($the_file); exit(); } else { // File doesn't exist. $page_title = 'File Download'; include ('./includes/header.php'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('./includes/footer.php'); } } include ('./includes/header.php'); $first = TRUE; // Initialize the variable. // Query the database. $query = "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM " . DB_UPLOADS . " ORDER BY date_entered DESC"; $result = mysql_query ($query); // Display all the URLs. while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { $bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee'); // If this is the first record, create the table header. if ($first) { echo '<div style="text-align: center;"><small>To use the links below, right click the File Name, Copy link location.<br />When dowloading files, you must SAVE the file with it\'s proper name.</small></div>'; echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="20%"><font size="+1">File Name</font></td> <td align="left" width="40%"><font size="+1">Description</font></td> <td align="center" width="20%"><font size="+1">File Size</font></td> <td align="left" width="20%"><font size="+1">Upload Date</font></td> </tr>'; $first = FALSE; // One record has been returned. } // End of $first IF. // Display each record. echo " <tr bgcolor=\"' . $bg . '\"> <td align=\"left\"><a href=\"./download.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td> <td align=\"left\">" . stripslashes($row['description']) . "</td> <td align=\"center\">{$row['fs']}kb</td> <td align=\"left\">{$row['d']}</td> </tr>\n"; } // End of while loop. // If no records were displayed... if ($first) { echo '<div align="center">There are currently no files to be viewed.</div>'; } else { echo '</table>'; // Close the table. } include ('./includes/footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596682 Share on other sites More sharing options...
iarp Posted July 22, 2008 Author Share Posted July 22, 2008 I changed header ('Content-disposition: attachment; filename=' . $fn . '\n'); to use single quotes and list ($fn, $ft, $fs) = mysql_fetch_array ($result); i changed ($result, ASSOC); now all the names are _n Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596703 Share on other sites More sharing options...
iarp Posted July 22, 2008 Author Share Posted July 22, 2008 Topic solved. New download script: <?php # Script 12.10 - download_file.php // This pages handles file downloads through headers. // Check for an upload_id. if (isset($_GET['uid'])) { $uid = (int) $_GET['uid']; } else { // Big problem! $uid = 0; } require_once ('./includes/mysql_connect.php'); // Connect to the database. include ('./includes/header.php'); if ($uid > 0) { // Do not proceed! // Get the information for this file. $query = "SELECT file_name, file_type, file_size FROM " . DB_UPLOADS . " WHERE upload_id=$uid"; $result = mysql_query ($query); list($fn, $ft, $fs) = mysql_fetch_array ($result); // Determine the file name on the server. $the_file = './files/' . $fn; $the_file2 = './files/' . $uid; // Check if it exists. if (file_exists ($the_file)) { // Send the file. output_file($the_file, $fn, $ft); } else { // File doesn't exist. $page_title = 'File Download'; include ('./includes/header.html'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('./includes/footer.html'); } } output_file(); function output_file($file, $name, $mime_type='') { /* This function takes a path to a file to output ($file), the filename that the browser will see ($name) and the MIME type of the file ($mime_type, optional). If you want to do something on download abort/finish, register_shutdown_function('function_name'); */ echo "<br><br>file: $file <br>name: $name <br> mime: $mime_type<br><br>"; if(!is_readable($file)) die('File not found or inaccessible!'); $size = filesize($file); $name = rawurldecode($name); /* Figure out the MIME type (if not specified) */ $known_mime_types=array( "pdf" => "application/pdf", "txt" => "text/plain", "html" => "text/html", "htm" => "text/html", "exe" => "application/octet-stream", "zip" => "application/zip", "doc" => "application/msword", "xls" => "application/vnd.ms-excel", "ppt" => "application/vnd.ms-powerpoint", "gif" => "image/gif", "png" => "image/png", "jpeg"=> "image/jpg", "jpg" => "image/jpg", "php" => "text/plain", "rtf" => "application/msword" ); if($mime_type==''){ $file_extension = strtolower(substr(strrchr($file,"."),1)); if(array_key_exists($file_extension, $known_mime_types)){ $mime_type=$known_mime_types[$file_extension]; } else { $mime_type="application/force-download"; }; }; @ob_end_clean(); //turn off output buffering to decrease cpu usage // required for IE, otherwise Content-Disposition may be ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header('Content-Type: ' . $mime_type); header('Content-Disposition: attachment; filename="'.$name.'"'); header("Content-Transfer-Encoding: binary"); header('Accept-Ranges: bytes'); /* The three lines below basically make the download non-cacheable */ header("Cache-control: private"); header('Pragma: private'); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // multipart-download and download resuming support if(isset($_SERVER['HTTP_RANGE'])) { list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2); list($range) = explode(",",$range,2); list($range, $range_end) = explode("-", $range); $range=intval($range); if(!$range_end) { $range_end=$size-1; } else { $range_end=intval($range_end); } $new_length = $range_end-$range+1; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range-$range_end/$size"); } else { $new_length=$size; header("Content-Length: ".$size); } /* output the file itself */ $chunksize = 1*(1024*1024); //you may want to change this $bytes_send = 0; if ($file = fopen($file, 'r')) { if(isset($_SERVER['HTTP_RANGE'])) fseek($file, $range); while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length) ) { $buffer = fread($file, $chunksize); print($buffer); //echo($buffer); // is also possible flush(); $bytes_send += strlen($buffer); } fclose($file); } else die('Error - can not open file.'); die(); } Quote Link to comment https://forums.phpfreaks.com/topic/116040-solved-problem-with-download-script/#findComment-596888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.