Deleting files after download in laravel - php

I'm creating a application that lets a user download a file. After the download i want the file to be deleted. The end of my code is like this:
return Response::download(public_path() . '/uploads/_tmp/' . urldecode($filename));
which means that the function ends on the return an i am not able to delete the file. I have tried to call a 'after' filter on the route but then the file gets deleted to quickly.
Any ideas?

You can use deleteFileAfterSend http://laravel.com/docs/5.0/responses#other-responses
return response()->download($filePath, $fileName, $headers)->deleteFileAfterSend(true);

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);

Simply use this line of code:
return response()->download($file_path)->deleteFileAfterSend(true);
Here, inside download function the file path will be passed as an argument. Let's say for an example you want to backup your database in a file and also want to delete with downloading:
$date = Carbon::now()->format('Y-m-d_h-i');
$pub_path = public_path();
$file_path = $pub_path . '/application/db_backups/' . $date . '.sql';
$output = shell_exec('mysqldump -h58.84.34.65 -uwsit -pwsit97480 websms > ' . $file_path);
return response()->download($file_path)->deleteFileAfterSend(true);

If you want to delete the source file after downloading, simply write it as follows
return response()->download($filePath)->deleteFileAfterSend(true);

$filename = storage_path() . '/testing.txt';
App::finish(function($request, $response) use ($filename)
{
unlink($filename);
});
return Response::download($filename);

For Laravel 5.8 use stream download. In the callback function, delete the file after echo the contents.
Let's take a look at the solution:
return response()->streamDownload(function () use ($pathToTheFile) {
echo Storage::get($pathToTheFile);
Storage::delete($pathToTheFile);
}, 'my-awesome-file-name');
I don't know if it works for the oldest or the latest versions.

unlink('./path/filename.extension');

Related

Laravel: Refresh response()->file

In Laravel I use this route
Route::get('admin/showBill/{file}','Admin\FileController#showBill');
and this code
class FileController extends AuthController
{
public function showBill($file)
{
$path = storage_path('app/bills/' . basename($file) );
if(!\File::exists($path)) return back();
return response()->file($path);
}
to display a pdf from my storage folder.
So if I have the pdf bill-1.pdf in my /storage/app/bills/ folder, then I can view it with the url
example-domain.com/admin/showBill/bill-1.pdf
The problem is that if I open that pdf with the browser, replace it, and refresh (F5) the page, then the old bill is shown. I guess its because its stored in the cache. Can I force Laravel to show the new replaced file?
I tried
public function showBill($file)
{
$path = storage_path('app/bills/' . basename($file) );
if(!\File::exists($path)) return back();
$path .= '?v='. time();
return response()->file($path);
}
But then Laravel tells me that this file does not exist. I am looking for a solution where I have not to rename the pdf file.
Are you sure you're replacing the right file?
If so, place this dd(). I've created an endpoint, response an empty pdf file - viewed it - replaced it with a content-filled pdf file and it works just fine when I replace it.
Edit: Also, you should validate the $file variable, using either a formrequest or validating in the controller.
public function showBill($file)
{
$path = storage_path('app/bills/' . basename($file));
if(!\File::exists($path)) {
dd("Quite possibly the problem is here, on the redirect back");
}
return response()->file($path);
}

How to use Laravel Storage::file();

I wanted to check the files inside my public path 'public/img/certs' but it returns an array. Im currently using laravel 5.5 and my first time using the 'Storage' file system.
use Illuminate\Support\Facades\Storage;
public function index(Request $request)
{
$path = base_path() . '/public/img/certs';
$files = Storage::files($path);
dd($files);
return view('dashboard.index');
}
try this code, reference link
$files = File::allFiles($directory);
foreach ($files as $file)
{
echo (string)$file, "\n";
}
First, $files = Storage::files('$path'); won't work at all, because the variable won't be interpreted.
Second, Storage::files will return an array because you asked for files and gave it a directory. The array will contain all the files in that directory.
Third, consider the public_path helper for this:
$path = public_path('img/certs');
It's a bit cleaner and will work if you ever put your public files somewhere non-standard.
Try below code in your controller hope this will help you to store the file in storage location of your application.
if($request->hasFile('image')){
$image_name=$request->image->getClientOriginalName();
$request->image->storeAs('public',$image_name);
}
$request->user()->profile_pic=$request->image;
$request->user()->save();
return back();
Modified it according to your requirement so this code work definately.

Symfony - BinaryFileResponse returns a ResponseHeader, but no file

currently trying to serve a static PDF file that is not within the web directory ($reasons).
I applied
$filepath = $this->getTargetFile($level, $name);
$response = new BinaryFileResponse($filepath);
$response->headers->set('Content-Type', 'application/pdf');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$filename
);
return $response;
and Profiler shows that there is a Response with correct headers, but there is no document view and no download prompt whatsoever. Is there something I am missing?
/** Get target file */
public function getTargetFile($level, $name)
{
return $this->kernel->locateResource($staticPath . $level . '/' . $name);
}
Some nerves were spent, but:
I created a form via JavaScript and submitted the needed parameters into a new window (thanks to this), that made the job. If anyone has a "cleaner" solution, I'm open to suggestions :)

Silverstripe Image Upload is changing name

I am uploading an image and while storing the image, I am setting the Filename like 'assets/Uploads/54f092af271b9.png' but after saving, the Filename fields loses some part. It becomes 'assets/54f092af271b9.png' losing the "Uploads/" directory altogether. Is it supposed to happen?
Here's the codes:
<?php
$img = new Image();
$baseName = pathinfo($file, PATHINFO_BASENAME);
$fileName = 'assets/Uploads/' . $baseName;
var_dump($fileName);
$img->Name = $baseName;
$img->Filename = $fileName;
$img->OwnerID = ($memberID = Member::currentUserID()) ? $memberID : 0;
$img->write();
var_dump($img->Filename); exit;
Output is:
assets/Uploads/54f092af271b9.png
assets/54f092af271b9.png'
Any ideas?
I was able to replicate the issue with the code you provided. After a bit of digging around, here is what I found.
It all starts in the onAfterWrite function in File class (which Image extends). Fired after you called write (obviously), this calls updateFilesystem where this line sets the Filename property with the result of the getRelativePath function call.
At the time of writing, getRelativePath looks like this:
public function getRelativePath() {
if($this->ParentID) {
// Don't use the cache, the parent has just been changed
$p = DataObject::get_by_id('Folder', $this->ParentID, false);
if($p && $p->exists()) return $p->getRelativePath() . $this->getField("Name");
else return ASSETS_DIR . "/" . $this->getField("Name");
} else if($this->getField("Name")) {
return ASSETS_DIR . "/" . $this->getField("Name");
} else {
return ASSETS_DIR;
}
}
Looking at that code, the issue you have comes from ParentID not being set on your record when you wrote it to the DB so the second condition is run instead returning the result of ASSETS_DIR . "/" . $this->getField("Name").
So that is the problem addressed, now for a solution. Silverstripe wants a parent folder, you've just got to give it one.
Fortunately there is a great little function on the Folder class called find_or_make which does what the name says, either finds the folder record in the filesystem and DB or it will generate it for you.
Note: In my own testing, while I had an "Uploads" folder, I did not have a corresponding DB record so this function wrote that for me an returned the result.
I then used the result to give the image I was writing to the DB a ParentID and it made the second var_dump return the same value as the first.
This is all you need to add to your code before calling write:
$parentFolder = Folder::find_or_make('Uploads');
$img->setParentID($parentFolder->ID);

Creating a folder when I run file_put_contents()

I have uploaded a lot of images from the website, and need to organize files in a better way.
Therefore, I decide to create a folder by months.
$month = date('Yd')
file_put_contents("upload/promotions/".$month."/".$image, $contents_data);
after I tried this one, I get error result.
Message: file_put_contents(upload/promotions/201211/ang232.png): failed to open stream: No such file or directory
If I tried to put only file in exist folder, it worked. However, it failed to create a new folder.
Is there a way to solve this problem?
file_put_contents() does not create the directory structure. Only the file.
You will need to add logic to your script to test if the month directory exists. If not, use mkdir() first.
if (!is_dir('upload/promotions/' . $month)) {
// dir doesn't exist, make it
mkdir('upload/promotions/' . $month);
}
file_put_contents('upload/promotions/' . $month . '/' . $image, $contents_data);
Update: mkdir() accepts a third parameter of $recursive which will create any missing directory structure. Might be useful if you need to create multiple directories.
Example with recursive and directory permissions set to 777:
mkdir('upload/promotions/' . $month, 0777, true);
modification of above answer to make it a bit more generic, (automatically detects and creates folder from arbitrary filename on system slashes)
ps previous answer is awesome
/**
* create file with content, and create folder structure if doesn't exist
* #param String $filepath
* #param String $message
*/
function forceFilePutContents ($filepath, $message){
try {
$isInFolder = preg_match("/^(.*)\/([^\/]+)$/", $filepath, $filepathMatches);
if($isInFolder) {
$folderName = $filepathMatches[1];
$fileName = $filepathMatches[2];
if (!is_dir($folderName)) {
mkdir($folderName, 0777, true);
}
}
file_put_contents($filepath, $message);
} catch (Exception $e) {
echo "ERR: error writing '$message' to '$filepath', ". $e->getMessage();
}
}
i have Been Working on the laravel Project With the Crud Generator and this Method is not Working
#aqm so i have created my own function
PHP Way
function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
{
$exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
array_pop($exploded);
$directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
if (!file_exists($directoryPathOnly))
{
mkdir($directoryPathOnly,0775,true);
}
file_put_contents($fullPathWithFileName, $fileContents);
}
LARAVEL WAY
Don't forget to add at top of the file
use Illuminate\Support\Facades\File;
function forceFilePutContents (string $fullPathWithFileName, string $fileContents)
{
$exploded = explode(DIRECTORY_SEPARATOR,$fullPathWithFileName);
array_pop($exploded);
$directoryPathOnly = implode(DIRECTORY_SEPARATOR,$exploded);
if (!File::exists($directoryPathOnly))
{
File::makeDirectory($directoryPathOnly,0775,true,false);
}
File::put($fullPathWithFileName,$fileContents);
}
I created an simpler answer from #Manojkiran.A and #Savageman. This function can be used as drop-in replacement for file_put_contents. It doesn't support context parameter but I think should be enough for most cases. I hope this helps some people. Happy coding! :)
function force_file_put_contents (string $pathWithFileName, mixed $data, int $flags = 0) {
$dirPathOnly = dirname($pathWithFileName);
if (!file_exists($dirPathOnly)) {
mkdir($dirPathOnly, 0775, true); // folder permission 0775
}
file_put_contents($pathWithFileName, $data, $flags);
}
Easy Laravel solution:
use Illuminate\Support\Facades\File;
// If the directory does not exist, it will be create
// Works recursively, with unlimited number of subdirectories
File::ensureDirectoryExists('my/super/directory');
// Write file content
File::put('my/super/directory/my-file.txt', 'this is file content');
I wrote a function you might like. It is called forceDir(). It basicaly checks whether the dir you want exists. If so, it does nothing. If not, it will create the directory. A reason to use this function, instead of just mkdir, is that this function can create nexted folders as well.. For example ('upload/promotions/januari/firstHalfOfTheMonth'). Just add the path to the desired dir_path.
function forceDir($dir){
if(!is_dir($dir)){
$dir_p = explode('/',$dir);
for($a = 1 ; $a <= count($dir_p) ; $a++){
#mkdir(implode('/',array_slice($dir_p,0,$a)));
}
}
}

Categories