问题
I have a file with directory: PDF\9783790820874-c1.pdf
I would like to delete this file with unlink()
funciton. But it seems like not working if I set the directory into a variable and unlink it.
For example:
$FileToDelete = "PDF\9783790820874-c1.pdf";
unlink($FileToDelete);
The code is logic isn't it? but why when I execute it, it show me error message:
Warning: unlink(PDF\9783790820874-c1.pdf ): Invalid argument on line 36
I have to save file directory into a variable to work well with my program, is there any way to solve it?
回答1:
Your backslash is being interpreted as an Escape Sequence.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Either change it to a forward slash (which does work for paths on Windows):
$FileToDelete = "PDF/9783790820874-c1.pdf";
Or use single quotes:
$FileToDelete = 'PDF\9783790820874-c1.pdf';
Or just escape the backslash:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
回答2:
I presume you are on Windows based on the backslash path separator.
I think the problem is due to escaping backslash causing problems, try:
$FileToDelete = "PDF\\9783790820874-c1.pdf";
unlink($FileToDelete);
Hopefully that will fix it for you.
Remember that '' and "" behave differently in relation to escaping within the string. So while 'a\b.pdf' might work "a\b.pdf" could mean something completely different (where a and b are [some string]).
来源:https://stackoverflow.com/questions/29037941/unlink-function-invalid-argument