Search code examples
phpunlink

unlink() Function invalid argument


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?


Solution

  • 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";