如何使用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 ...
原文:
最满意答案
由于这个问题被标记为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_PEEKTEXT
和PTRACE_PEEKDATA
实际上是相同的功能,所以你可以在PTRACE_PEEKDATA
代码中PTRACE_PEEKDATA
一个地址,例如,在PTRACE_PEEKTEXT
的堆栈上给一个地址,它将完全愉快地复制回来为你。 区别在于OS /处理器组合,其中存储器在DATA存储器和CODE存储器之间“分离”。 大多数现代操作系统和处理器都没有做出这种区分。 同样明显适用于PTRACE_POKEDATA
和PTRACE_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 memoryPTRACE_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 ofset variable x = 7
, ifx
happens to be in a register)In Linux, since memory is "all the same",
PTRACE_PEEKTEXT
andPTRACE_PEEKDATA
are actually the same functionality, so you can give an address in code forPTRACE_PEEKDATA
and an address, say, on the stack forPTRACE_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 toPTRACE_POKEDATA
andPTRACE_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 ...
-
带ptrace的内存调试器(Memory debugger with ptrace)[2022-04-13]
由于以下几个原因,使用调试器和捕获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 ...
-
C ++内存调试器?(C++ Memory Debugger?)[2023-07-13]
对于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 ...
-
如何在Linux上附加到像调试器这样的运行进程?(How do I attach to a running process like a debugger on linux?)[2023-03-29]
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的函数,它允许一个进程(在检查特权之后)以各种方式检查另一个进程。 这是一个电话,但第一个参数是“你想做 ...