unlink() Function invalid argument

岁酱吖の 提交于 2019-12-24 14:19:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!