PHP备份&;下载
我想为我的服务器上的文件和文件夹创建备份脚本,我发现:PHP备份&;下载,php,backup,temp,Php,Backup,Temp,我想为我的服务器上的文件和文件夹创建备份脚本,我发现: # Name of the buckup file $filename = 'BACKUP_' . date('Y.m.d') . '.tar.gz'; # Target path where to save the file $targetPath = '/path/to/backup/'; # Directory to backup $dir = "/path/to/files/"; # Execute the tar comm
# Name of the buckup file
$filename = 'BACKUP_' . date('Y.m.d') . '.tar.gz';
# Target path where to save the file
$targetPath = '/path/to/backup/';
# Directory to backup
$dir = "/path/to/files/";
# Execute the tar command and save file
system( "tar -pczf ".$targetPath ."".$filename." ".$dir );
这很好,然后我可以将URL传递给脚本和正确的头并下载文件
我的问题是,我将经常运行此操作,不希望将备份保留在服务器上。所以我想找到一种创造性的方法来创建一个被删除的临时文件。问题是一些文件可能相当大,我无法知道文件何时完全下载。在数据库中添加文件名/路径和创建的时间戳,编写一个cron作业以删除任何超过您希望保留时间的内容。每小时30分钟运行一次。无论你觉得有多频繁都是必要的
至于是否已下载,请创建下载管理器/下载/file/id,其中id可以是该表中的主键,只有在尚未下载时才删除,除非已经下载48小时,或者类似的内容。您可以在/tmp中创建备份文件,然后使用:
# Name of the buckup file
$filename = 'BACKUP_' . date('Y.m.d') . '.tar.gz';
# Target path where to save the file
$targetPath = '/path/to/backup/';
# Directory to backup
$dir = "/path/to/files/";
# Execute the tar command and save file
system( "tar -pczf ".$targetPath ."".$filename." ".$dir );
readfile("{$targetPath}{$filename}"); // outputs file contents to a browser
unlink("{$targetPath}{$filename}"); // removes file from disk
阅读更多信息
PS
此外,您还可以使用ZipArchive类(内置)更灵活地创建自己的存档。或者不使用数据库,只需在文件名中包含一个时间戳。可能的情况是,您必须扫描目录,如果它是一个高流量站点,与数据库相比,这将使用大量I/O。而且,将无法跟踪下载。