I am wondering how I would set a default period for a file to expire? At the moment you enter the number of minutes you want the file to be active, and after that set time the file expires. But, I want to add some functionality so that, by default, the file will expire after 10 minutes, say. Any help would be much appreciated. I've got an idea of how to do it. I'm just not sure about the coding of it.
File Name: upload.php
<!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 a File</title> </head> <body> <?php include('core/inc/init.inc.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'], "core/files/{$_FILES['file']['name']}"); echo "<p>". $_FILES['file']['name'] ." has been successfully uploaded.<p>"; } ?> <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>
File Name: file_list.php
<?php include('core/inc/init.inc.php'); $files = mysql_query("SELECT file_id, file_name, file_expiry FROM files"); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>File List</title> </head> <style type="text/css"> table { border-collapse:collapse; width:600px; } td, th {border: solid 1px #999; padding: 4px;} </style> <body> <table> <tr> <th>File Name</th> <th>Expiry</th> </tr> <?php while(($row = mysql_fetch_assoc($files))!==false ){ ?> <tr> <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> </body> </html>
File Name: download.php
<?php include('core/inc/init.inc.php'); if (isset($_GET['file_id'])){ $file_id = (int)$_GET['file_id']; $files = mysql_query("SELECT file_name, file_expiry FROM files WHERE file_id={$file_id}"); if (mysql_num_rows($files) !=1){ echo "Invalid File ID"; }else{ $row = mysql_fetch_assoc($files); if($row['file_expiry'] < time()) { echo "This file has expired"; } else { $path = "core/files/{$row['file_name']}"; 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); } } } ?>