Temporary download link

Ask about a PHP problem here.
Post Reply
adityasaxena
Posts: 1
Joined: Tue Aug 28, 2012 9:36 pm

Temporary download link

Post by adityasaxena »

My code does not work ! help - I can upload files, it goes into database and file list is OK. However when I download it does not work. I get a blank file downloaded. Why is this happening?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Download</title>
</head>

<body>
<?php include('downloadcore/inc/init.inc.php'); ?>

<?php
if (isset($_GET['file_id']))
{
	$file_id = (int)$_GET['file_id'];
	//echo $file_id;
	$file = mysql_query("SELECT file_name, file_expiry FROM files WHERE file_id={$file_id}");
	
	if (mysql_num_rows($file) != 1)
	{
		echo "Invalid File ID";
	}
	else
	{
		$row = mysql_fetch_assoc($file);
		
		if($row['file_expiry'] < time())
		{
			echo "This file has expired";
		}
		else 
		{
			$path = "downloadcore/files/{$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);
		}
	}
}
?>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File List</title>
</head>

<body>
<?php require_once('downloadcore/inc/init.inc.php'); ?>

<?php 
$time = (int)time();
//echo $time;

//Here we create a list of files that have not expired till now and can be downloaded.
$files = mysql_query("SELECT file_id, file_name, file_expiry FROM files WHERE file_expiry > {$time}");
//die(mysql_error());
?>


<div align="center">
<table width="950" align="center" border="1" >
  <tr align="center">
  	<th>FILE NAME</th>
	<th>EXPIRES</th>
  </tr>
  
  <?php 
  	while (($row = mysql_fetch_assoc($files)) !== false)
	{
  ?>
  	<tr align="center">
		<td><a href="download.php?file_id=<?php echo $row['file_id']; ?>">		
		<?php echo $row['file_name']; ?></a></td>
		<td><?php echo date('d/m/Y H:i:s', $row['file_expiry']); ?></td>
	</tr>
  <?php
	}
  ?>
</table>
</div>

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload</title>
</head>

<body>
<?php require_once('downloadcore/inc/init.inc.php'); ?>

<?php
if (isset($_POST['expiry'], $_FILES['file']))
{
	$file_name = mysql_real_escape_string($_FILES['file']['name']);
	$expiry = time() + ((int)$_POST['expiry']*60);
	
	mysql_query("INSERT INTO files (file_name, file_expiry) VALUES ('{$file_name}', {$expiry})");
	//die(mysql_error());
	
	move_uploaded_file($_FILES['file']['tmp_name'], "downloadcore/files/{$_FILES['file']['name']}");
}
?>
<div>
	<form action="" method="post" enctype="multipart/form-data">
		<p>
			<input type="text" name="expiry" />
		</p>
		<p>
			<input type="file" name="file" />
		</p>
		<p>
			<input type="submit" value="upload"/>
		</p>
	</form>
</div>

</body>
</html>
Last edited by Temor on Thu Aug 30, 2012 11:07 pm, edited 1 time in total.
Reason: Code tags
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Temporary download link

Post by FrederickGeek8 »

Are you able to directly download it? Did it upload at all?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Temporary download link

Post by Helx »

When you upload, don't you need do CHMOD to 777 ?

That's where I've always gone wrong... Could be different though.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Temporary download link

Post by jacek »

You should not have any HTML on the download page (first code block) since that will stop the header()s from being sent.
Image
Post Reply