在 php unlink 中使用通配符
我目前正在制作一个 php 脚本来绘制数据库中的一堆数据,将其排列到文本文件中,然后运行 GNUPlot 脚本来生成图表。我已经完成了所有这些工作,现在我需要做的就是删除我不再需要的文本文件。
我一直在尝试的内容是从不同论坛上的另一个帖子中获得的:
foreach( glob('US_A.2.6.*') as $file )
{
unlink($file);
}
但是,问题是它不起作用。这些文件具有复杂的结束名称:
- US_A.2.6.1.1a.txt
- US_A.2.6.1.2a.txt
- US_A.2.6.1.3a.txt
- US_A.2.6.1.4a.txt
- US_A.2.6.1.5a.txt
- US_A.2.6 .1.6a.txt
等等。
I am currently making a php script to graph a bunch of data from a DB, arrange it into text files and then run a GNUPlot script to generate graphs. I have all of this working , now all I need to do is delete the text files I don't need anymore.
What I have been trying was gotten from another thread on a different forum:
foreach( glob('US_A.2.6.*') as $file )
{
unlink($file);
}
The problem, however, is that it doesn't work. The files have complex end names:
- US_A.2.6.1.1a.txt
- US_A.2.6.1.2a.txt
- US_A.2.6.1.3a.txt
- US_A.2.6.1.4a.txt
- US_A.2.6.1.5a.txt
- US_A.2.6.1.6a.txt
And more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 getcwd() 检查您的工作目录。如果您的文本文件不在同一目录中,则需要指定路径。
另外,尝试回显 glob() 语句的输出以查看是否找到任何文件:
您没有检查 unlink() 返回值,因此如果文件不可写,它可能会默默失败(取决于您的 error_reporting 级别) 。
Check your working directory with getcwd(). If you are not in the same directory as your text files, you will need to specify the path.
Also, try echoing the output of the glob() statement to see if it is finding any files:
You are not checking the unlink() return value, so it could be failing silently (depending on your error_reporting level) if the files are unwritable.
根据您的限制,通过使用
exec()
调用,您也许能够生成更简洁的代码,但代价是跨平台可移植性较差。我更喜欢给出绝对路径,我通常将其硬编码到所有脚本中包含的配置文件的变量中,因此:或者
此解决方案适用于 Linux 和其他 UNIX 系统。尽管在 Windows 上使用 PHP 并不常见,但您可以对其进行调整以使其在 Windows 服务器上工作。
如果存在任何用户提交的输入,则使用
exec()
会带来一些严重安全风险;按原样使用代码,就可以了,因为路径和文件名都是硬编码的。但如果您包含任何自定义输入,请确保使用 escapeshellarg功能。正如 George Cummins 指出的那样,该解决方案不包括错误处理。我发现如果您需要逐个文件进行错误处理,他的解决方案会更好。然而,我通常使用像这里这样的设置,没有错误处理,用于清理自动生成的文件,并且在使用此类构造的 10 多年中我从未遇到过任何问题。对于此类用途,通常只需要在设置和测试期间进行错误处理,并且通常归结为正确设置目录权限。
Depending on your constraints, you may be able to generate more concise code at the cost of it being less cross-platform portable, by using the
exec()
call. I prefer giving absolute paths, which I usually hard-code in variables in a configuration file included in all scripts, so:Or
This solution would work on linux and other UNIX systems. Although it's uncommon to use PHP on Windows, you could adapt it to work on Windows servers.
There are some critical security risks associated with using
exec()
if there is any user-submitted input; with the code as-is, it will be fine as the paths and filenames are all hardcoded. But if you include any custom input, make sure to use the escapeshellarg function.As George Cummins notes, this solution doesn't include error handling. I find his solution is better if you need error handling on a file-by-file basis. However, I commonly use setups like the one here, with no error handling, for cleanup of automatically generated files, and I have never run into any problems in 10+ years of using such constructs. For such uses, error handling is often only needed during setup and testing, and it usually comes down to setting directory permissions correctly.