简体   繁体   中英

Laravel 4 - Rename downloaded files on the fly

I have a form on my app that allows users to upload a file.. That file gets given a random name, saved to the file system and the details including the original name of the file and the random name of the file gets saved to the database.

When the user clicks the link to re-download the file, i want the file to be renamed to the original file name on the fly but kept as the random name in the file system... if that makes sense.

The code i'm using to download the file at the moment is as follows

// Download the document
$file = public_path() . '/uploads/' . $document->userid . '/' . $document->storedname;
return Response::download($file);

The random filename that is created on upload is $document->storedname

The original name of the file is stored as $document->originalname

将其作为第二个参数传递给download方法:

return Response::download($file, $document->originalname);
$newName = "download.pdf";
            $filename="samplepdf.pdf";
            $path = storage_path("app/public/" . $filename);
          
            if (!\File::exists($path)) {
                return response()->json(['status' => false], 404);
            }
            
            return response()->download($path,$newName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM