Ok, Here is what I am trying to do. I am going to explain very clearly, if you have any questions, please reply.
I want it so that when a user goes to download a file, any file, PHP will create another file in a "temp" folder With the actual file name. This sounds confusing, I know, but follow this...(Note: I am using mysql)
1) User wants to download "Something.zip"
2) The encrypted filename on server is "{0A7ACDA3-1425-4174-955E-8756DE0877F9}.zip"
3) Make PHP copy the file replacing the name with the MySQL entry for that particular name.
4) So, it would make "{0A7ACDA3-1425-4174-955E-8756DE0877F9}.zip" be renamed to "Something.zip" (From MySQL).
5) Let the person have 3 hours to download the file.
6) After 3 hours is up on server time, delete "Something.zip"
7) And same thing for other files.
Here is what I have came up with for the copying part, I cant figure out how to delete the files though after 3 hours.
<?php
/* Connect and Select Database */
$dbh = mysql_connect("localhost","home","password");
mysql_select_db("downloads",$dbh);
/* Gets ID from a link (ex: www.domain.com/download.php?id=1) */
$id = htmlspecialchars(addslashes($_GET['id']));
/* MySQL query */
$query = mysql_query("SELECT * FROM Downloads WHERE id='$id'");
/* Check if ID exists. */
$check = mysql_num_rows($query);
/* If $check returned 1 or more rows (Note: It can only return one row. Did this to be safe) */
if($check >= 1)
{
/*
Note:
$row['link'] is equal to "{0A7ACDA3-1425-4174-955E-8756DE0877F9}.zip"
$row['name'] is equal to "Something.zip"
*/
/* Get array from ID. */
$row = mysql_fetch_array($query);
/* Get old file name to move from */
$file = $row['link'];
/* Get extension from old filename (ex ".zip" or ".rar" (I only host these archives))
$extension = substr($row['link'], -4);
/* Copy old file and rename with the real name from MySQL database to "temp" folder.
Temp folder is already made and CHMOD 777. */
$newname = "temp/".$row['name'].$extension;
/* If the file was successfully named and copied */
if(copy($row['link'],$newname))
{
/*
Now, somewhere in here is when the "session" should
start to run on the servers side instead of the clients side to
automatically delete the temp file after 3 hours.
I think we should use sesions...Not too sure...
I am wanting to do this instead of using the "Hotlink" option in cPanel
- Which to me is very unuseful for me right now, so please dont bring that up...
*/
/* Display download link */
echo "Your download is ready. Click <a href=\"".$newname."\">Here</a>.";
}
else
{
/* Copy/Rename was unsuccessful. */
echo "Sorry, there was an internal error. Please try again later, or report it to the admins.";
}
}
else
{
/* If $count is 0 */
echo "This download does not exist.";
}
?>
So, if you have any ideas of how the server will delete the temp file "Something.zip" after 3 hours, Please let me know and post again if you will! Thanks in advance, -Robert 🙂