Php 下载到root,然后从数据库中删除文件

Php 下载到root,然后从数据库中删除文件,php,database,download,Php,Database,Download,我有这个功能,它会自动从我的数据库中删除一定年龄后的文本文件 $r = new textfiles; $db = new DB; $currTime_ori = $db->queryOneRow("SELECT NOW() as now"); ... if($this->site->textfilesretentiondays != 0) { echo "PostPrc : Deleting textfiles older than

我有这个功能,它会自动从我的数据库中删除一定年龄后的文本文件

$r = new textfiles;
$db = new DB;
$currTime_ori = $db->queryOneRow("SELECT NOW() as now");

...

if($this->site->textfilesretentiondays != 0)
        {
            echo "PostPrc : Deleting textfiles older than ".$this->site->textfilesretentiondays." days\n";

            $result = $db->query(sprintf("select ID from textfiles where postdate < %s - interval %d day", $db->escapeString($currTime_ori["now"]), $this->site->textfilesretentiondays));      
            foreach ($result as $row)
                $r->delete($row["ID"]);
        }
$r=新文本文件;
$db=新db;
$currTime_ori=$db->queryOneRow(“选择现在()作为现在”);
...
如果($this->site->textfilesreptiondays!=0)
{
echo“postrc:删除早于“$this->site->textfilesreptiondays.”天的文本文件\n;
$result=$db->query(sprintf(“从postdate<%s-间隔%d天的文本文件中选择ID,$db->escapeString($currTime_ori[“now”]),$this->site->textfilesreptiondays));
foreach($结果为$行)
$r->删除($row[“ID”]);
}
现在我将编辑此函数,以便首先将所有文本文件自动下载到根目录
/www/backup
,然后脚本应使用字符串
$r->delete($row[“ID”])删除文本文件


目前,我不知道如何实现这一点。

对我来说,由于信息泄露,似乎不可能完全回答您的问题

您是将整个文件内容存储在数据库中,还是仅存储路径和文件名? 它将帮助我们查看“
$row
”的内容,它表示数据库中的一行


如果只存储文件名(以及可选的路径),可以使用php中的“copy”(复制)函数将文件复制到备份目录。请注意,您必须确保执行脚本或运行web服务器的用户具有写入目录的权限


您可以将此功能添加到类
textfiles
中,方法类似于
makeBackup

信息很少,但我会尝试一下。如果要在删除行之前备份行,可以使用插入FOREACH循环中的以下代码,以json_编码的形式将行存储在.txt文件中,然后执行delete命令:

$myfile = fopen("/www/backup/".$row["ID"].".txt", "w") or die("Unable to open file!");
$txt = json_encode($row);
fwrite($myfile, $txt);
fclose($myfile);
按你的方法

function delete ($id){
   $result = $db->query(sprintf("select * from textfiles where id=$id);

   //if you have filepath use copy as SebTM suggested
   $path = $row['path']; //assuming path is the column name in ur db
   $filename = basename($path); //to get filename
   $backup_location = '/www/backup/'.$filename;
   copy($path, $backup_location);

   //if you have data in db
   $content = $row['data'] //assuming data to be backed up to file is in a field 'data'
   $backup_location = '/www/backup/file.txt';
   file_put_contents($backup_location, $content);
}

但这并不是最理想的方法,您甚至可以将初始查询转换为上面的delete函数,并且只调用delete函数一次,而不是在循环中调用它

什么是文本文件类?你能从数据库中选择文件的路径吗?所有这些都运行在几个.php上。有些人可以访问数据库,然后进一步处理上面的代码段。这不是根目录,不知道文件系统如何映射到URL,我们就不知道路径是什么。如果文件已经在服务器上,那么“下载”是什么意思?为什么要使用昂贵的数据库查询来确定时间?为什么在后续查询中使用该值作为参数,而不是仅使用
now()
?这是一个非常混乱的问题。如果您的
delete()
方法按预期工作,那么为什么不创建一个
backup()
方法来执行与文件类似的操作,而是使用PHP的
copy()
函数在删除之前对其进行备份?您确定需要复制和删除吗。。也许可以用move?您的sql查询
从postdate<%s-间隔%d天的文本文件中选择ID
可以这样修改
从postdate
。Thx@all获取帮助!现在,我正在处理这些提议,而来自阿米尔的贡献对我来说是最有帮助的,并且给了他奖金。