How can the file path be properly specified when using unlink() to delete a file in PHP?
When using unlink() to delete a file in PHP, the file path must be properly specified to ensure the correct file is deleted. The file path should be relative to the directory where the PHP script is located or an absolute path. It is important to double-check the file path to avoid accidentally deleting the wrong file.
$file_path = '/path/to/file.txt';
if (file_exists($file_path)) {
unlink($file_path);
echo 'File deleted successfully.';
} else {
echo 'File not found.';
}
Keywords
Related Questions
- How can SQL Injections be prevented in PHP code, especially when dealing with user input like search queries?
- What is the significance of using references in the callback function for array_reduce?
- What are the advantages of using classes or Composer packages for handling calendar event data in PHP, compared to manual coding as shown in the forum thread?