Temporary download link
Posted: Tue Aug 28, 2012 9:41 pm
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>