在 php unlink 中使用通配符

发布于 2024-11-17 04:59:22 字数 469 浏览 3 评论 0原文

我目前正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-11-24 04:59:22

使用 getcwd() 检查您的工作目录。如果您的文本文件不在同一目录中,则需要指定路径。

另外,尝试回显 glob() 语句的输出以查看是否找到任何文件:

echo $file . PHP_EOL;
unlink($file);

您没有检查 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:

echo $file . PHP_EOL;
unlink($file);

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.

じее 2024-11-24 04:59:22

根据您的限制,通过使用 exec() 调用,您也许能够生成更简洁的代码,但代价是跨平台可移植性较差。我更喜欢给出绝对路径,我通常将其硬编码到所有脚本中包含的配置文件的变量中,因此:

define (FULL_DIRECTORY_PATH,'/whatever/your/path/is/');

...

exec('rm ' . FULL_DIRECTORY_PATH . 'US_A.2.6.*');

或者

define (BASE_PATH,'/base/path/');

...

exec('rm ' . BASE_PATH . 'additional/path/US_A.2.6.*');

此解决方案适用于 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:

define (FULL_DIRECTORY_PATH,'/whatever/your/path/is/');

...

exec('rm ' . FULL_DIRECTORY_PATH . 'US_A.2.6.*');

Or

define (BASE_PATH,'/base/path/');

...

exec('rm ' . BASE_PATH . 'additional/path/US_A.2.6.*');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文