Danny971's avatar

File Name Change Not Reflected in Downloaded File - Laravel Storage Issue

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!
0 likes
4 replies
Sergiu17's avatar

You also need to rename the physical file which is located in your storage folder, changing the name in your DB is not enough.

$filename = $req->input('filename');

$fileID = Upload::find($id);

$oldFileName = $fileID->filename;

$fileID->filename = $filename;
$fileID->save();

Storage::move(
	storage_path('app/public/' . $oldFileName), 
	storage_path('app/public/' . $filename)
);
Danny971's avatar

@Sergiu17 i tried your code but no luck could you give me the full thing like integrate it form me maybe i did something wrong i get the following error

Unable to create a directory at C:\xampp\htdocs\pos_cfr\storage\app\C:/xampp/htdocs/pos_cfr/storage/app/public/uploads.

Sergiu17's avatar

@Danny971 I can't give you full code, because I don't have it. I think the problem is because of Windows. try this

Storage::move(
	storage_path("app\public\$oldFileName"), 
	storage_path("app\public\$filename")
);

Please or to participate in this conversation.