Question

So this one is pretty straight forward I want to delete a file on the server using PHP, I have:

$myfile = 'theone.png';
unlink($myfile);

This code deletes the file, howevere if the path to file is /images/theone.png, it doesn't work, I have tried images\theone.png with no luck.

If I try and connect with FTP I get the error message to say that cURL does not support the unlink function... Any help would be great.

Thanks Guys!

Was it helpful?

Solution

What about:

$root = realpath($_SERVER['DOCUMENT_ROOT']);
$myfile = '$root/images/theone.png';
unlink($myfile);

Although to my knowledge, your attempted method should work, unless either I'm missing something, or you haven't included some code here that might be interfering with the unlink.

OTHER TIPS

__DIR__ - this magic constant contains current directory, in case that the file is in the same directory as your PHP script you can use:

unlink(__DIR__ . "/$myfile");

If the file is for example in one directory above your PHP script you can use:

unlink(__DIR__ . "/../$myfile");

If the directory has correct access rights it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top