首页 \ 问答 \ 如何使用php删除目录中的旧文件(how to delete old files in a directory using php)

如何使用php删除目录中的旧文件(how to delete old files in a directory using php)

我有一个数据库备份目录,以及一个每天备份数据库的PHP脚本。 但是一个月后我会在备份目录中有30个文件。 如何编写每天删除旧备份文件的PHP脚本,以便我的目录中总有10个备份文件? 存储新备份时,将删除最旧的备份文件,依此类推......


I have a directory for database backups, and a PHP script that makes a backup of the database every day. But after a month or so i will have 30 files in the backup directory. How can i write a PHP script that deletes old backup files every day so that there are always 10 backup files in my directory? when a new backup is stored, the oldest backup file will be deleted and so on ...


原文:
更新时间:2022-12-01 15:12

最满意答案

由于这个问题被标记为Linux和Unix,我将对David Scwartz所说的内容进行一些扩展,简而言之,就是“操作系统中有一个API”。 Windows的相同基本原理也适用,但实际的实现是不同的,尽管我怀疑操作系统内部的实现是一样的,但没有真正的方法可以知道,因为我们无法检查Windows的源代码(然而,人们可以理解操作系统和处理器的工作原理,并找出必须发生的事情!)

Linux有一个名为ptrace的函数,它允许一个进程(在检查特权之后)以各种方式检查另一个进程。 这是一个电话,但第一个参数是“你想做什么”。 以下是一些最基本的例子 - 对于较少的“常见”操作,有几十个其他例子:

  • PTRACE_ATTACH - 连接到进程。
  • PTRACE_PEEKTEXT - 查看附加进程的代码内存(例如,反汇编代码)
  • PTRACE_PEEKDATA - 查看附加进程的'数据存储器(显示变量)
  • PTRACE_POKETEXT - 写入进程的代码内存
  • PTRACE_POKEDATA - 写入进程'数据存储器。
  • PTRACE_GETREGS - 复制当前寄存器值。
  • PTRACE_SETREGS - 更改当前的寄存器值(例如,如果x恰好在寄存器中,则set variable x = 7的调试命令)

在Linux中,由于内存“完全相同”, PTRACE_PEEKTEXTPTRACE_PEEKDATA实际上是相同的功能,所以你可以在PTRACE_PEEKDATA代码中PTRACE_PEEKDATA一个地址,例如,在PTRACE_PEEKTEXT的堆栈上给一个地址,它将完全愉快地复制回来为你。 区别在于OS /处理器组合,其中存储器在DATA存储器和CODE存储器之间“分离”。 大多数现代操作系统和处理器都没有做出这种区分。 同样明显适用于PTRACE_POKEDATAPTRACE_POKETEXT

所以,假设“调试程序”使用:

long data = ptrace(PTRACE_PEEKDATA, pid, 0x12340128, NULL); 

当使用PTRACE_PEEKDATA为地址0x12340128调用OS时,它将“查看”内存的相应内存映射,位于0x12340128(页面对齐,使得0x12340000),如果存在,它将被映射到内核,数据是然后从地址0x12340128复制到本地存储器,内存未映射,复制的数据作为返回值传回。

该手册规定使用的启动如下:

父进程可以通过调用fork(2)并让生成的子进行PTRACE_TRACEME来启动跟踪,然后(通常)执行exec(3)。 或者,父母可以使用PTRACE_ATTACH开始跟踪现有过程。

对于几页,有关man ptrace更多信息。


Since the question is tagged Linux and Unix, I'll expand a little on what David Scwartz says, which in short is "there is an API for that in the OS". The same basic principle applies in Windows as well, but the actual implementation is different, and although I suspect the implementation inside the OS does the same thing, there's no REAL way to know that, since we can't inspect the source code for Windows (one can, however, understanding how an OS and a processor works, sort of figure out what must be happening!)

Linux has a function called ptrace, that allows one process (following some checking of privileges) to inspect another process in various ways. It is one call, but the first parameter is a "what do you want to do". Here are some of the most basic examples - there are a couple of dozen others for less "common" operations:

  • PTRACE_ATTACH - connect to the process.
  • PTRACE_PEEKTEXT - look at the attached process' code memory (for example to disassemble the code)
  • PTRACE_PEEKDATA - look at the attached process' data memory (to display variables)
  • PTRACE_POKETEXT - write to process' code memory
  • PTRACE_POKEDATA - write to process' data memory.
  • PTRACE_GETREGS - copy the current register values.
  • PTRACE_SETREGS - change the current register values (e.g. a debug command of set variable x = 7, if x happens to be in a register)

In Linux, since memory is "all the same", PTRACE_PEEKTEXT and PTRACE_PEEKDATA are actually the same functionality, so you can give an address in code for PTRACE_PEEKDATA and an address, say, on the stack for PTRACE_PEEKTEXT and it will perfectly happily copy that back for you. The distinction is made for OS/processor combinations where memory is "split" between DATA memory and CODE memory. Most modern OS's and processors do not make that distinction. Same obviously applies to PTRACE_POKEDATA and PTRACE_POKETEXT.

So, say that the "debugger process" uses:

long data = ptrace(PTRACE_PEEKDATA, pid, 0x12340128, NULL); 

When the OS is called with a PTRACE_PEEKDATA for address 0x12340128 it will "look" at the corresponding memory mapping for the memory at 0x12340128 (page-aligned that makes 0x12340000), if it exists, it will get mapped into the kernel, the data is then copied out from address 0x12340128 into the local memory, the memory unmapped, and the copied data passed back as the return value.

The manual states the initiating of the usage as:

The parent can initiate a trace by calling fork(2) and having the resulting child do a PTRACE_TRACEME, followed (typically) by an exec(3). Alternatively, the parent may commence trace of an existing process using PTRACE_ATTACH.

For several pages more information do man ptrace.

相关问答

更多
  • 一位同事结束了使用DTE的解决方案,并将代码发布到了PasteBin上。 感兴趣的方法是AttachVisualStudioToProcess和TryGetVsInstance 源代码 public static void AttachVisualStudioToProcess(Process visualStudioProcess, Process applicationProcess) { _DTE visualStudioInstance; if (TryGetVsInstance( ...
  • 这是不可能的。 看到这里 : 对于仅限管理的和仅限本地的调试,您只能将1个调试器附加到进程。 为什么? 本地调试器从托管调试器的下面窃取调试事件。 这使管理调试器混淆并导致其崩溃。 本地调试器无法在此处与托管调试器协调。 That's not possible. See here: For both managed-only and native-only debugging, you can only attach 1 debugger to a process. Why? The native debu ...
  • 读取TempDataDictionary的对象时,将在该请求结束时将其标记为删除。 这意味着如果你把某些东西放在TempData上 TempData["value"] = "someValueForNextRequest"; 而另一个请求,您访问它,该值将在那里,但一读取它,该值将被标记为删除: //second request, read value and is marked for deletion object value = TempData["value"]; //third request ...
  • 由于以下几个原因,使用调试器和捕获malloc / free调用是不切实际的: 从一个流程切换到另一个流程的开销对于非平凡的程序来说非常重要。 与其他方法相比,您最终会花费相同数量的内存来存储所有权信息。 (这是我真正希望改进的) 有很多函数可以在堆中运行,并且很容易错过一些。 It is not practical to use a debugger and trap malloc/free calls for a few of reasons: The overhead of switching fro ...
  • 从命令提示符使用wmic,CreationDate将列出所有正在运行的进程的创建时间 time:\>wmic process get processid , name , creationdate | grep opera 20140616162551 opera.exe 1108 time:\>tlist 1108 | grep Stack 1108 opera.exe windows - Process Creation Time from Process ...
  • 对于Windows中的内存泄漏,我发现启用了用户模式堆栈跟踪的UMDH非常有用。 这是一个教程 。 有关其他工具的列表,请查看此处 。 For memory leaks in Windows, I've found that UMDH with usermode stack traces enabled is pretty useful. Here's a tutorial. For a list of other tools, look here.
  • 点击这里 这将检查JDWP 。 基本上: boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean(). getInputArguments().toString().indexOf("-agentlib:jdwp") > 0; Check here. This checks for the JDWP. Basically: boolean isDebug = java.lang.management.Man ...
  • ptrace()是您正在寻找的系统调用。 ptrace() is the system call you're looking for.
  • _CrtSetDbgFlag控制调试堆的检查类型。 您的代码(或者您正在使用的库)可能会提高检查级别。 例如,您可以在每次分配和释放时检查堆的完整性。 这可能导致巨大的放缓。 除非您确实需要它来查找特定的内存问题,否则请勿使用_CRTDBG_CHECK_ALWAYS_DF 。 对于基本的泄漏检查(通常不会造成巨大的性能损失),您只需要(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF) 。 I tried another solution. Setting the e ...
  • 由于这个问题被标记为Linux和Unix,我将对David Scwartz所说的内容进行一些扩展,简而言之,就是“操作系统中有一个API”。 Windows的相同基本原理也适用,但实际的实现是不同的,尽管我怀疑操作系统内部的实现是一样的,但没有真正的方法可以知道,因为我们无法检查Windows的源代码(然而,人们可以理解操作系统和处理器的工作原理,并找出必须发生的事情!) Linux有一个名为ptrace的函数,它允许一个进程(在检查特权之后)以各种方式检查另一个进程。 这是一个电话,但第一个参数是“你想做 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。