Deleting a file after user download it
unlink($filename);
This will delete the file.
It needs to be combined with ignore_user_abort()
Docs so that the unlink
is still executed even the user canceled the download.
ignore_user_abort(true);
...
unlink($f);
Deleting a File from Storage after Download
There are two ways you can do this.
Let's assume you have a file in storage/app/download.zip
:
1. Because Laravel uses Symfony's HttpFoundation internally, you can use the deleteFileAfterSend
method:
public function download()
{
return response()
->download(storage_path('app/download.zip'))
->deleteFileAfterSend(true);
}
2. Create a Terminable Middleware that deletes the file after the download response was prepared.
class StorageDownload
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
\Storage::delete('download.zip');
}
}
You'll need to register the middlware and assign it to your route for it to work.
As for triggering the download using JavaScript, something as trivial as setting the window location will work:
axios
.post('files/export/' + file.id, formData)
.then(function() {
window.location = 'files/download/' + file.id
});
Don't worry, this will not navigate away from your current page, it will just trigger the download.
Delete file after using response.download() in node.js?
response.download
has a callback function, you can delete your file after download as below
response.download(filePath, yourFileName, function(err) {
if (err) {
console.log(err); // Check error if you want
}
fs.unlink(yourFilePath, function(){
console.log("File was deleted") // Callback
});
// fs.unlinkSync(yourFilePath) // If you don't need callback
});
delete file from the server after download in php
There is no way to know when the user finished downloading a file with PHP, I'd use a queue system to delete the file after n seconds of the request:
How to Build a PHP Queue System
Deleting a local file after download has finished
In the meantime I figured one can just encodeURI the contents of the csv file to download it without the need to write to a file on the disk. I'm rather new to web development, so I didn't know this was an option. Here is the code snippet for any other beginner like me, who encounters this problem (csv_name and csv_contents are sent from the bakcend)
function setCsvToDownload(csv_name, csv_contents) {
var temp_download_link = document.createElement("a");
temp_download_link.download = csv_name;
temp_download_link.href = 'data:text/csv;charset=utf-8' + encodeURI(csv_contents);
temp_download_link.target = '_blank';
temp_download_link.click();
}
delete a file after download in codeigniter
Sorry I was not reading, but the problem with your code is that it is missing the closing )
public function remove_downloaded($file_name)
{
if(file_exists($file_name)) // here is the problem
{
unlink($file_name);
}
}
Also the way the force download works is that anything after will not run. I would suggest using an ajax call after the controller.
UPDATE:
But as you have mentioned in your comment, you can delete the file before creating it as mentioned in this post Unlink after force download not working Codeigniter
Delete a file after download it using nodejs
You can call fs.unlink()
on the finish
event of res
.
Or you could use the end
event of the file stream:
var file = fs.createReadStream(fileName);
file.on('end', function() {
fs.unlink(fileName, function() {
// file deleted
});
});
file.pipe(res);
Deleting files after download in laravel
I personally use the following;
$response = Response::make(file_get_contents($path_to_file), $status_code, $headers);
Status code is obviously the code which you want to return.
Within the $header parameter you can pass an array with the indexes Content-Type and Content-Disposition.
Then you can simply unlink $path_to_file and return $response.
Much easier way of deleting a file would be to use Jon's answer for versions of Laravel > 4.0.
You can use deleteFileAfterSend
http://laravel.com/docs/5.0/responses#other-responses
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
How to delete a file after downloading it with Response-download() in Laravel
Use the deleteFileAfterSend
method:
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
File Download and Responses
//controller class
public function downloadAndDeleteCSV{
...
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);
}
Related Topics
PHP Errors Not Being Displayed in the Browser [Ubuntu 10.10]
How to Generate an Excel Document with Multiple Worksheets from PHP
Get the Last Modified Date of a Remote File
Difference Between & and && in PHP
Laravel 5 - After Login Redirect Back to Previous Page
Fatal Error: Call to Undefined Function Imap_Open() in PHP
Laravel Eloquent, Group by Month/Year
Edit Google Calendar Events from Google Service Account: 403
How to Use Include Within a Function
413 Request Entity Too Large - File Upload Issue
How to Store an Array into MySQL
How to Build Unlimited Level of Menu Through PHP and MySQL
Laravel 5.4 Field Doesn't Have a Default Value