1

我在这里有一个问题,我不确定它是否可能,但我想在用户离开页面时删除一个文件......我目前有一个 php 脚本(见下文),它处理文件的删除,但是我不知道页面卸载时如何运行它?

<?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"; 
    }
}
?>

有任何想法吗?

4

2 回答 2

1

您的主脚本/应用程序必须跟踪每个服务器请求存储在数据库中的用户活动。

每 x 分钟运行一次由 cron 作业安排的 php 脚本,向 DB 询问最后的用户活动(日期/时间)。在 y 分钟无活动后删除文件。

只是一个想法。

于 2012-06-01T15:28:12.093 回答
1

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:

  1. Using your database to track which users have access to which videos, and how how long
  2. On a request to the video page, assert that the user has the right to access that file and, if the assertion passes, generate a signed url which is valid only for a short period of time and which may be used in the video page to deliver your movie.

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.

于 2012-06-01T15:31:17.747 回答