I have an issue here, I'm not sure if it's possible but I would like to delete a file when the user leaves the page... I currently have a php script (see below) which handels the deleting of a file, but i don't know how to run it when the page unloads?
<?php
include 'js/db/db.php';
$film = $_GET['film'];
$movie = $_GET['org_film'];
$user = $_COOKIE['user'];
$filename = "movies/".$user."/".$film.".mp4";
if (file_exists($filename)) {
unlink($filename);
header('location: storage.php');
}
else{
$filename = "movies/".$user."/".$film.".m4v";
if (file_exists($filename)) {
unlink($filename);
header('location: storage.php');
}
else{
echo " file doesn't Exist";
}
}
?>
Any ideas?
From your code it looks like you're creating a temporary file so that you can deliver a video to a specific user for a specific amount of time. This is a highly inefficient approach. You'd be better off:
A signed URL would look something like:
http://path.to/your/movie.m4v?timestamp=2309583240&signature=234p9345u234234092wjdfl
Where timestamp is the current UNIX timestamp and signature is a SHA1 hash of the timestamp and some secret known only to your application.
Amazon S3 supports this feature - you could also store the movie files on Amazon S3 with limited read privileges, and use one of the many popular S3 libraries to generate time-limited signed URLs for your users.
This would obviate the need to shift large files around the disk, and thereby greatly increase performance. It would also scale better as the maximum concurrent viewers would no longer by tied to disk size.