Temporary File Download System - Not downloading correctly

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Temporary File Download System - Not downloading correctly

Post by TerryHarvey »

Hi.

I'm having a little trouble. I followed the "Temporary File Download System" tutorial but I decided to leave out the expiry time. Here is my download page code:
<?php  
require ('core/inc/init.inc.php');

if(isset($_GET['file'])) {
	$file = (int)$_GET['file'];

	$sql = mysql_query("SELECT * FROM `files` WHERE `file_id` = {$file}");

	if (mysql_num_rows($sql) != 1) {
		echo 'That file does not exist';
	} else {
		$row = mysql_fetch_assoc($file);

		$path = "core/file/{$row['file_name']}";

		header('Content-Type: application/octetstream');
		header('Content-Type: application/octet-stream');
		header('Content-Description: File Transfer');
		header("Content-Disposition: attachment; filename=\"{$row['file_name']}\"");
		header('Content-Length: '.filesize($path));

		readfile($path);
	}
}
?>
However, when I try to download a file, I get the option to download the file "download.php" which contains the errors:
<br /><b>Warning</b>:  mysql_fetch_assoc() expects parameter 1 to be resource, integer given in <b>C:\xampp\htdocs\PHP\SQL\Download MOD\download.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for core/file/ in <b>C:\xampp\htdocs\PHP\SQL\Download MOD\download.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  readfile(core/file/) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>C:\xampp\htdocs\PHP\SQL\Download MOD\download.php</b> on line <b>22</b><br />
What am I doing wrong?

Thanks.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Temporary File Download System - Not downloading correct

Post by jacek »

Add
echo mysql_error();
after the query causing the error, the MySQL server should then tell you what went wrong.
Image
User avatar
TerryHarvey
Posts: 20
Joined: Thu Mar 01, 2012 11:12 am

Re: Temporary File Download System - Not downloading correct

Post by TerryHarvey »

I already tried that, it just displayed the same error as the one in the downloaded file.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Temporary File Download System - Not downloading correct

Post by jacek »

TerryHarvey wrote:I already tried that, it just displayed the same error as the one in the downloaded file.
hmm, you must have put it in the wrong place :? You get the other errors because the query fails and returns false so the has to be an error :s

Can you post the code with the mysql_error() added ?
Image
Post Reply