I am trying to save a pdf and other documents file like Docx and video in the database using a base64.
what I want to do is to revert the changes from base64 to the exact file. is it possible to do that? thank you and stay safe.
I know it is a duplicate question, but I cannot find any answer in laravel.
You can decode and then pass to Storage put method
Storage::disk('public')->put('file.pdf',base64_decode($base64encodedstring));
don't forget to import Storage facade
use Illuminate\Support\Facades\Storage;
$filename = 'upload/profile_pic/' . md5(uniqid(rand(), true)) . '_' . $input['document']['filename'];
file_put_contents($filename, base64_decode($input['document']['base64']));
$input['document'] = $filename;
Decode file and save the file in the public path, create upload folder in the public folder and save any file.
Related
I'm using stripe to get invoice
I need to store this invoice in my public storage folder - I'm using laravel
stripe does not return pdf file path like this - http://localhost/app/invoice.pdf
instead of that
they provide a url like this - https://pay.stripe.com/invoice/acct_1sflasjfmsklagbs/test_alkfnsajklgdbnajkgsbnajkfbsuayasgtjbwq8tuy23690q8319qr8busajvbssavkjsabngukwjqhr82rhadsawoilafkn/pdf?s=ap
so when you enter above url - it generates a pdf dynamically
I tried this and it works if you have direct path of pdf file
$contents = file_get_contents('http://localhost/app/invoice.pdf');
\Storage::disk('public')->put('filename.pdf', $contents);
but below given code does not work
$contents = file_get_contents('https://pay.stripe.com/invoice/acct_1sflasjfmsklagbs/test_alkfnsajklgdbnajkgsbnajkfbsuayasgtjbwq8tuy23690q8319qr8busajvbssavkjsabngukwjqhr82rhadsawoilafkn/pdf?s=ap');
\Storage::disk('public')->put('filename.pdf', $contents);
anyone can please assist here?
In my Laravel project I need to upload mp3 files, However Laravel using mpga as a mime type to validate mp3 files type I found that in this answer.
$results = Validator::make($request->all(), [
"song" => "required|file|mimes:mpga|max:8192",
]);
I am okay with that but my problem is the file is stored with mpga extension, I know the reason behind this weird action from this answer.
How I store the file
// Upload the song
$filePath = $request->song->store("public/songs");
But I want to store the file with mp3 extension.
Without seeing your code, the best I can suggest is to use the putFileAs() method from the Storage class.
This method allows you to specify a file name when storing your file:
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
// Manually specify a file name:
Storage::putFileAs(
'folder',
new File('/path/to/uploaded-music.mp3'),
'stored-song-name.mp3'
);
See Laravel doc: https://laravel.com/docs/5.8/filesystem#storing-files
Edit: If you want to keep Laravel random file naming just like the putFile() method does, you can generate a random string and append your extension:
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
// Manually specify a file name:
Storage::putFileAs(
'folder',
new File('/path/to/uploaded-music.mp3'),
Str::random(40) . '.mp3'
);
If you look at Laravel source code, this is how putFile() does it to generate a random file name.
Lobsterbaz's answer works perfectly, and i also want to give alternative to that answer which will help you to keep your file's extension as it is.
Laravel changes mp3 file's extension as mpga after uploading, so here i will show you how to keep mp3 file's extension as it is after uploading, below code will help you to do it:
use Illuminate\Support\Str;
$path = $request->file('song')->storeAs(
'songs', Str::random(40).".mp3"
);
Congrats, Now your song will be stored as mp3, but be sure to upload mp3 files only and put validation of mimes for mp3 otherwise it can break the file.
I want to compress the uploaded image and store the base64 of the image in the database. Every time I google the question I always gets the idea to use the image stored in a certain path. I'm using intervention/image package
I have tried this code
$img = Image::make($request->photo)->resize(300, 200);
//get the extension
$type = $request->file('photo')->getClientOriginalExtension();
//convert it into the base64.
$base64 = 'data:image/' . $type . ';base64,' . base64_encode(file_get_contents($img));
I want to store the photo in the database after compressing and converting into the base64 format. Error that I got
You need not write any compression or conversion code.
Try adding encode('data-url')
I hope it works.
$img = Image::make($request->photo)->resize(300, 200)->encode('data-url');
I'm using storage_path() for storing uploaded images, but when I use it is pointing wrong on my page.
I use it like this {{ $data->thumbnail }} where $data came from the database and thumbnail comes as the string which used storage_path
Let us take a look at the default L4 application structure:
app // contains restricted server-side application data
app/storage // a writeable directory used by L4 and custom functions to store data ( i.e. log files, ... )
public // this directory is accessible for clients
If I were you, I would upload the file to the public directory directly:
Store image here: public_path() . 'img/filename.jpg'
Save the 'img/filename.jpg' in database
Generate the image URL with url('img/filename.jpg') => http://www.your-domain.com/img/filename.jpg
Hope this helps.
The storage_path function returns the path to the storage folder, which is inside the app folder --and outside the public folder-- so it's not directly accessible from the client, that's why your images are not being displayed. You can move them to the public folder path, or you could use a custom controller to handle the image requests, read the image from the storage folder and return the value.
I am having Problem in saving pdf generated by mPDF to a specific folder. Every thing is working fine with pdf generation, Only I am not able to save it to a local folder. Can any one help me out with that?
Short answer is to put the full path where you plan to save the file. Like so...
$mpdf->WriteHTML($html);
$mpdf->Output('/etc/home/JohnWayne/example/pdf/','F');
I found the solution. This is very simple. As for as mPDF library is concerned, it does not parse the base_url(). Instead we have to work with $_SERVER['DOCUMENT_ROOT']; In my case i have done as; Open the config.php in Application/config folder and insert the following;
$config['file_path']=$_SERVER['DOCUMENT_ROOT']."my_project/file/";
Now it is a peace of cake to call the config; Here is I have done in the controller;
$this->mpdf->Output($this->config->item('file_path')."invoice/arif.pdf",'F');
The Problem is solved. Do let me know if there is any technical or logical issue with my code.
You can use write file helper for this
$this->load->helper('file');
write_file('my_pdf_file.pdf',$generated_pdf);
File Helper
For this follow these simple steps. Follow BASEPATH instead of APPPATH
$path = BASEPATH . 'file/invoice';
if(is_dir($path)){
$this->mpdf->Output(realpath($path).'arif.pdf','F');
}else{
echo 'error';
}
EDITS :
Here is an alternative solution which you might like
Go to application/config/constants.php and add constant there
define('FILE_PATH' , $_SERVER['DOCUMENT_ROOT']."my_project/file/");
Then use it like this.
$this->mpdf->Output(FILE_PATH ."invoice/arif.pdf",'F');
make a folder in your root directry
$mpdf->WriteHTML($html);
$mpdf->Output(FCPATH.'PDF/Broker.pdf','F');
echo $Ledger_Group_Report = base_url().'PDF/Broker.pdf';