Yii2. Download file from database BLOB field - download

I saved a png image to BLOB field in database.
$model->content = \yii\web\UploadedFile::getInstanceByName($fileName);
$model->save();
How can I download it now? I tried:
$model=$this->findModel($id);
header('Content-length: 362654');
header('Content-Type: png');
header('Content-Disposition: attachment; filename='.$model->name_);
echo $model->content;
But after this the file was downloaded but with the size 1 kb instead 362654. And nothing inside file.

I found out a solution.
With just a:
$model->content = \yii\web\UploadedFile::getInstanceByName($fileName);
it is not possible to save content of file as getInstanceByName does not return it. In order to get content and save it in db BLOB field I used following code:
$file = \yii\web\UploadedFile::getInstanceByName($fileName);
$fp = fopen($file->tempName, 'r');
$content = fread($fp, $file->size);
fclose($fp);
$model->content = $content;
$model->save();
So the problem was incorrect saving of the file.

Related

PDF not generated in laravel

I'm generating pdf using following code in Laravel but the issue is sometimes data is saved in the database and pdf is not generated when i check the pdf folder there is no pdf files with that name, this does not happens every time i generate the pdf but there are some entries in my database for which the pdf is not generated, i want some way so that i can check whether the pdf has been generated and mark status true in the database if pdf is generated later on recreate pdf and send it to user through cron jobs or is there any other way also, Please suggest some solution Thanks
use PDF;
..
..
$DataStore->user_id = $user_id;
$DataStore->description = $description;
$DataStore->save();
if($DataStore) {
$pdf = PDF::loadView('/admin/user_submission_pdf', ['data' => $DataStore]);
$content = $pdf->download()->getOriginalContent();
Storage::put('public/pdfFiles/' . $DataStore->Id . '.pdf', $content);
to download your pdf, change the the line
$content = $pdf->download()->getOriginalContent();
to
$content = $pdf->download('file-name-you-want.pdf');
then at last line should return:
return $content;

Laravel $request->getContent() returns empty string

I'm posting a CSV file from one Laravel app to another:
Sending:
$contents = file_get_contents($filePath);
Log::debug('contents', ['contents'=>$contents]); // I can see contents of file
Http::withToken($token)->attach('attachment', $contents)->post($uri);
Receiving:
$content = $request->getContent();
Log::debug('about to store content to file...', ['content'=>$content]);
// about to store content to file... {"content":""}
What am I missing here?
Files are not the content, you should fetch the file out with.
$request->file('attachment');
If you want to send a file as a raw request body, this snippet shows you how from the documentation.
$response = Http::withBody(
file_get_contents($filePath), 'text'
)->post($uri);

Generate multiple PDF documents with loop with dompdf in laravel

I am trying to download multiple pdf using dompdf in laravel, but below solution just concatenate into one pdf file
$weeks = ['test','test2'];
$html = '';
foreach($weeks as $hours)
{
$view = view('contract');
$html .= $view->render();
}
$pdf = PDF::loadHTML($html);
$sheet = $pdf->setPaper('a4', 'landscape');
return $sheet->download('download.pdf');
the server can only return a response, if you want several pdf files you should modify your method so that it accepts some parameter and for each parameter generate a pdf
It is not possible to download more than one file in a single HTTP request.
What you could do is pack the files into o zip and download that. You can try https://github.com/zanysoft/laravel-zip or any other available solution.
Another option would be to save the files and return only paths for the saved files back to the client. Then make a new request and download the file for each of the returned filepaths.
Also if you want to save to separate PDF files you need to move PDF creation into your foreach loop (create a PDF for each parameter).
$weeks = ['test','test2'];
$files = [];
foreach($weeks as $hours)
{
$view = view('contract');
$html = $view->render();
$pdf = PDF::loadHTML($html);
$pdf->setPaper('a4', 'landscape');
$pdf->save('documents/'.$hours.'.pdf');
$files[] = 'documents/'.$hours.'.pdf';
}
return response()->json(['files' => $files]);
In this case the foreach doesn't really do anything other than produce two identical PDF files. If you want to actually apply some kind of changes to your view based on values in $weeks you need to pass that to your view.
$view = view('contract', ['data' => $hours]);
That makes $data available in your view and you can change the resulting PDF in that way (show your contract.blade.php if you need more help regarding that).

Why my .jpg files are saved as .tmp?

I am following a tutorial from Laracasts, for uploading files. Here in the example I will upload a jpg file or png file, and in my images folder I get a file with different name and with a different extension. Why is that?
This is how I am moving the uploaded file.
And here is how the files are saved.
You better send also the filename:
Input::file('photo')->move($destinationPath, $fileName);
You'll probably be able to (not tested)
Input::file('photo')->move($destinationPath, Input::file('photo')->getClientOriginalName());
Also, check the docs: http://laravel.com/docs/requests#files
You may try this (No need to use public_path):
public function store()
{
$targetPath = 'images'; // For "public/images"
$file = Input::file('image'); // If "image" is the name of the file input
$filename = $file->getClientOriginalName();
$file->move($targetPath, $fileName);
}

Laravel 4 get image from url

OK so when I want to upload an image. I usually do something like:
$file = Input::file('image');
$destinationPath = 'whereEver';
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
if( $uploadSuccess ) {
// save the url
}
This works fine when the user uploads the image. But how do I save an image from an URL???
If I try something like:
$url = 'http://www.whereEver.com/some/image';
$file = file_get_contents($url);
and then:
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
I get the following error:
Call to a member function move() on a non-object
So, how do I upload an image from a URL with laravel 4??
Amy help greatly appreciated.
I don't know if this will help you a lot but you might want to look at the Intervention Library. It's originally intended to be used as an image manipulation library but it provides saving image from url:
$image = Image::make('http://someurl.com/image.jpg')->save('/path/saveAsImageName.jpg');
$url = "http://example.com/123.jpg";
$url_arr = explode ('/', $url);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$destinationPath = public_path().'/img/'.$name;
file_put_contents($destinationPath, file_get_contents($url));
this will save the image to your /public/img, filename will be the original file name which is 123.jpg for the above case.
the get image name referred from here
Laravel's Input::file method is only used when you upload files by POST request I think. The error you get is because file_get_contents doesn't return you laravel's class. And you don't have to use move() method or it's analog, because the file you get from url isn't uploaded to your tmp folder.
Instead, I think you should use PHP upload an image file through url what is described here.
Like:
// Your file
$file = 'http://....';
// Open the file to get existing content
$data = file_get_contents($file);
// New file
$new = '/var/www/uploads/';
// Write the contents back to a new file
file_put_contents($new, $data);
I can't check it right now but it seems like not a bad solution. Just get data from url and then save it whereever you want

Resources