skip to Main Content

To update a record with a photo, I have used the unlink command to delete the photo in the corresponding directory. I have already saved the file name and path separately in the database and to delete the file, I have written it as follows:

unlink($task->FilePath.$task->TaskFile);

but return this error:

unlink(/assets/TaskFiles/2024/04/921308991.png): No such file or directory

Considering that my default address is the public branch on the server, how do I add the default address to be placed at the beginning of the address?

2

Answers


  1. You’ll need to use the public_path function to get the full file path starting from the public directory

    unlink(public_path($task->FilePath.$task->TaskFile));
    
    Login or Signup to reply.
  2. You can use the Storage facade to handle file operations. Here’s how you can delete a file using the Storage facade in Laravel

    use IlluminateSupportFacadesStorage;
    $filePath = $task->FilePath . $task->TaskFile;
    
    // Check if the file exists before attempting to delete it
    if (Storage::disk('public')->exists($filePath)) {
        // Delete the file
        Storage::disk('public')->delete($filePath);
    } else {
        // File does not exist
        // Handle the case where the file doesn't exist, or log an error
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search