I have a Laravel application where users can upload files and change the file name. I'm encountering an issue where the changed file name is not reflected when downloading the file. some time when I download it I get failed no file found
Here is the code
public function saveNewDocumentName(Request $req, $id){
$filename = $req -> input('filename');
$fileID = upload::find($id);
if(!$fileID) {
return response() ->json(['error' => 'Document not found!'], 404);
}
if($fileID) {
$fileID -> filename = $filename;
$fileID -> save();
session()->flash('notification', ['type' => 'success', 'message' => 'Changes made successfully']);
} else {
session()->flash('notification', ['type' => 'error', 'message' => 'Error in making changes']);
}
return redirect()-> back();
}
I also tried this
public function saveNewDocumentName(Request $req, $id)
{
$filename = $req->input('filename');
$fileID = upload::find($id);
if (!$fileID) {
return response()->json(['error' => 'Document not found!'], 404);
}
$fileID->filename = $filename;
$fileID->save();
$customFileName = $filename; // use the edited filename
session()->flash('notification', ['type' => 'success', 'message' => 'Changes made successfully']);
$filePath = 'uploads/' . basename($fileID->document);
$fullPath = storage_path('app/public/' . $filePath);
if (file_exists($fullPath)) {
return response()->download($fullPath, $customFileName)->deleteFileAfterSend(true);
} else {
session()->flash('notification', ['type' => 'error', 'message' => 'File does not exist.']);
return redirect()->back();
}
}
The issue I'm facing is that after saving the changes to the file name, when I try to download the file, the original file name is displayed instead of the updated one. Additionally, sometimes I receive a "File does not exist" error when attempting to download the file.
I have confirmed that the file name is successfully updated in the database. I also checked the file path and it appears to be correct. I have verified that the file exists in the specified storage location.
I have tried various approaches, including using the response()->download() method with a custom file name parameter, but the issue persists.
I'm using Laravel 8 and have the storage configuration properly set up in my config/filesystems.php file.
Could anyone provide insights into why the changed file name is not reflected in the downloaded file? Any suggestions or alternative approaches would be greatly appreciated. Thank you!