Deleting files using wildcards - exec vs unlink

I am working on a PHP script where I want to delete some files from a given folder using a wildcard (*).
I found some working examples, for example this one where functions unlink()and are used glob().

Now I was wondering, would it also be ok to delete files using a function execand command like rm -f /path/to/folder/_prefix_*?
Are there any security risks associated with this?
And if everything is in order, would it be better in terms of performance?

EDIT:
So, from the first answers, I see that using execmight be an acceptable solution.
What about performance issues? Is there a chance that the option execmay be better (faster / less demanding) by the method glob/unlink?

Thank you in advance

+5
source share
2 answers

Since there is no way to enter user-provided data, there is no security issue when using execover glob/unlink. However, use glob/unlinkallows you to define exceptions:

foreach(glob("delete/*") as $f) {
    if( $f == "delete/notme.txt") continue;
    unlink($f);
}

And it execoften shuts down on shared servers, so it is glob/unlinkmore portable. If you have a special setting and you are not going to let it go, you do not need to worry about it.

+8

. , , exec .

, glob unlink.

+1

All Articles