I have below function to download a pdf file
public function download($id) {
$detail = pesanmakam::findOrFail($id);
$name = date('YmdHis') . ".pdf";
$data = PDF::loadView('guest/log/pdf', compact('detail'))->setPaper('a4')->setWarnings(false)->save('myfile.pdf');
return $data->download($name);
}
above download function works fine but it's just stay on the same page. Is it possible to redirect it to another page after the download succeed?
You can't because forcing download file is made with HTTP header and redirection is based on the same. So you can't do both at the same time.
You can find more information on this other topic here
Read it
public function download($id) {
$detail = pesanmakam::findOrFail($id);
$name = date('YmdHis') . ".pdf";
$data = PDF::loadView('guest/log/pdf', compact('detail'))->setPaper('a4')->setWarnings(false)->save('myfile.pdf');
// return $data->download($name);
return Redirect::to($url);//$url > where u want to go
}
Paste the following line just after the function call
header('Location: http://www.example.com/');
suppose you called function like
$down = download(10);
//then just below it write like
header('Location: http://www.example.com/');
instead of http://www.example.com put the page url where you want to redirect after download.
Try this:
public function download($id) {
$detail = pesanmakam::findOrFail($id);
$name = date('YmdHis') . ".pdf";
$data = PDF::loadView('guest/log/pdf', compact('detail'))->setPaper('a4')->setWarnings(false)->save('myfile.pdf');
$data->save('folder_name/'.$name)
return Redirect::to('url')
}
Related
Hello there
Hope you will be doing well.I want to redirect to a route after file download but as we know that return only works once in a controller method how i can achieve this with laravel 5.7.I have to set a session and display it when data exported in txt file.I want this with post method.
Every thing is fine but redirect is not working;
Controller Method
public function exportTxtProcess(Request $request)
{
$table = $request->tblExportSelect;
$destinationPath = public_path('/');
$result;
$outputs = DB::select("SELECT * FROM $table");
$today = date("Y-m-d");
$fileName = $table . "-" . $today;
$fp = fopen($destinationPath . "$fileName.txt", "wb");
foreach ($outputs as $output) {
$output = (array)$output;
#array_shift($output);
$removeUserId = #$output['user_id'];
$created_at = #$output['created_at'];
$updated_at = #$output['updated_at'];
if (($key = array_search($removeUserId, $output)) !== false) {
unset($output[$key]);
}
if (($key1 = array_search($created_at, $output))) {
unset($output[$key1]);
}
if (($key2 = array_search($updated_at, $output))) {
unset($output[$key2]);
}
if (is_null($created_at) OR $created_at == '') {
unset($output['created_at']);
}
if (is_null($updated_at) OR $updated_at == '') {
unset($output['updated_at']);
}
$netResult = $this->getTableFields($table, $output);
fwrite($fp, $netResult);
}
$result = fclose($fp);
if ($result) {
$pathToFile = $destinationPath . "$fileName.txt";
$redirect = redirect()->back();
$sess = Session::flash('success', 'Table exported successfully');
return response()->download($pathToFile)->deleteFileAfterSend(true);
}
}
Thank in advance
You can only have 1 response, so it's impossible to instruct a double
return. What you could do, is set the filename you wish to have
downloaded in a session variable, then redirect back to whatever page.
Within the redirected page, you could flash your message, along with
having an automatic download of the file.
Here is some threads on the topic:
How do I redirect after download in Laravel?
PHP generate file for download then redirect
It's my update function and it's not showing image when I echo $fileName. It's not working like it was I want to make it with the condition if there is any image then update that image otherwise leave that I tried many times but it's not working.
public function update(Request $request, Header $header)
{
if($request->hasFile('headerimg')){
$fileName = $request->file('headerimg')->getClientOriginalName();
$request->file('headerimg')->move('uploads/header',$fileName);
}
$header->title = $request->title;
$header->discription = $request->discription;
$header->keywords = $request->keywords;
$header->headerimg = $fileName;
$header->h2 = $request->h2;
$header->breadcumb = $request->breadcumb;
$header->category = $request->category;
$header->save();
return redirect()->route('dashboard.headers.index')->with('success', 'Packages Edited Successfully');
}
I guess that your problem is in your view.
Maybe you forget to use enctype="multipart/form-data" or your input name does not match with mapped controller variable.
For find the exact problem use dd($request) and dd($request->file('headerimg')) and see can you get request correct or not.
I use this code edit image and if image was not selected the old image will remain same without any error.
public function update(Request $request, Header $header)
{
$file = $request->headerimg;
$id = $request->id;
if($file == ''){
$arr['header'] = Header::select()->where($id);
$fileName = $header['headerimg'];
}
else{
$fileName = $request->file('headerimg')->getClientOriginalName();
$request->file('headerimg')->move('uploads/header',$fileName);
}
$header->title = $request->title;
$header->discription = $request->discription;
$header->keywords = $request->keywords;
$header->headerimg = $fileName;
$header->h2 = $request->h2;
$header->breadcumb = $request->breadcumb;
$header->category = $request->category;
$header->save();
return redirect()->route('dashboard.headers.index')->with('success', 'Page Edited Successfully');
}
How can I refresh the page after force_download in codeigniter controller, here is my controller function. It is not performing any function after calling the force_download.
public function download(){
$this->load->library('user_agent');
$this->load->helper('download');
$file_id = $this->uri->segment(3);
if(!empty($file_id)){
//get file info from database
$fileInfo = $this->Files->get_file($file_id);
foreach ($fileInfo as $key => $object) {
$download_direc = $object->file_direc;
}
//download file
force_download($download_direc, NULL);
//increment the download value by 1 in the db
$this->Files->update_download($file_id);
//refresh the page
redirect($this->agent->referrer());
}
}
Thank you
On the view page when user clicks your anchor link or button to download the file.
Just take the id of that button and put a jQuery interval to 2500-3000 so the page will refresh after 2.5-3 seconds.
you can try this. i hope it will help you.
public function download($fileName = NULL) {
if ($fileName) {
$file = realpath ( "download" ) . "\\" . $fileName;
if (file_exists ( $file )) {
$data = file_get_contents ( $file );
force_download ( $fileName, $data );
} else {
redirect ( base_url () );
}
}
}
PHP question on how to return a specific value from another class in a seperate file. So I have created a uploader class to handle and store files uploaded by users. But trying to connect the dots back to the orginal request which if for lets say a specific page - how can i pass a specific value back after I set the values in the other file... code example below.
Thanks Citti
//file 1 //store page request
public function store()
{
//pass the uploaded file to be uploaded and stored
$this->uploader->upload(RequestFile::file('assetfile'));
$page = new Page;
$page->user_id = $asset->user()->id;
$page->assetfile = $file->user_id; <--- ? see below
$page->save();
}
//file 2 //store physical file and create record in DB
class Uploader {
public function upload($file)
{
$extension = $file->getClientOriginalExtension();
$string = str_random(30);
$uploadName = $string . '.' . $extension;
$destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;
$file->move($destinationPath, $uploadName);
$file = new File;
$file->user_id = $asset->user()->id;
$file->assetfile = $uploadId;
$file->save();
$return $file->user_id; ?!?
What you could try is session variables?
Be sure to start sessions on both file 1 and file 2 via:
session_start();
Now, in your upload function, you can declare a session variable
$_SESSION['specificValue'] = $valueSetInUpload;
Now, back on your first file you can access the variable inside of your store function with
$valueSetInUpload = $_SESSION['specificValue'];
I believe this is what you are asking, but I could be wrong.
To return values from a function, you use "return value" and you just have to store that to a variable or output it. Something like:
public function store() {
$fileId = $this->uploader->upload(RequestFile::file('assetfile'));
$page = new Page;
$page->user_id = $asset->user()->id;
$page->assetfile = $fileId;
$page->save();
}
//file 2 //store physical file and create record in DB
class Uploader {
public function upload($file) {
$extension = $file->getClientOriginalExtension();
$string = str_random(30);
$uploadName = $string . '.' . $extension;
$destinationPath = storage_path() . '/app/uploads/' . Request::user()->id;
$file->move($destinationPath, $uploadName);
$file = new File;
$file->user_id = $asset->user()->id;
$file->assetfile = $uploadId;
$file->save();
return $file->user_id;
}
}
I have a function which takes two parameters data(html),name. In the function i am trying to save the data in PDF file on my localhost/abc-folder. Unfortunately it runs fine but don't write in specific file or so..
Here is my code.
<?php
function pdf($data, $name) {
if (count($name) > 1) {
$name = "Orders";
} else {
$name = 'Order_'.$name[0]['order_id'];
}
$pdf = new DOMPDF;
$pdf->load_html($data);
$pdf->render();
$str=$pdf->output();
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/aabcd.pdf","wb");
fwrite($fp,$str);
fclose($fp);
}
?>