PHP unlink function use absolute or relative path?

41 Views Asked by At

this one really puzzles me, if i use the code below in file delete_pic.php, it give me the Warning: unlink(/upload_pic/image.jpg): No such file or directory in /Sites/delete_pic.php

    <?php
$filePath = '/upload_pic/image.jpg';

if (unlink($filePath)) {
echo 'File deleted successfully.';
} else {
echo 'Failed to delete the file.';
}
?>

but if I replace with the relative path $filePath = 'upload_pic/image.jpg'; it works, "File deleted successfully." Both delete_pic.php and upload_pic folder are in root directory. <img src="/upload_pic/image.jpg"> displays correctly.

Can anyone help me understand? thanks a lot.

1

There are 1 best solutions below

0
Mohammed Hamid Hamoda On

You can use both absolute or relative paths:

  • when you prefix your path with / like this /upload_pic/image.jpg this is considered an absolute path.
  • when you don't prefix your path with '/' like this upload_pic/image.jpg this is considered a relative path.