How to store Excel file after generation on Laravel storage? - laravel

I use this code to generate binary file. How to store it in local storage?
$path = 'events/';
$filename = 'visitors_list_' . date('d-m-y') . '.xlsx';
return Excel::import(new VisitorsExport($request), $path.$filename);
I use library

The method is Excel::store().
According to this
Beware your return keyword will end the execution

Related

How to write csv file and download in browser?

In laravel 5.7 app I need to write csv file and download in in browsers download function using method :
\Response::download(
I try to upload it to tmp directory and checking value I see
$sys_get_temp_dir= sys_get_temp_dir();
line above has /tmp value
Next I save it as :
$dest_csv_file= $sys_get_temp_dir . '/box_rooms_'.time().'.csv';
$header = ["Id",... ];
$fp = fopen($dest_csv_file, "w");
fputcsv($fp, $header);
foreach ($storageSpacesCollection as $line) {
fputcsv($fp, $line);
}
fclose($fp);
But searching on my local ubuntu 18(on server I also have ubuntu) I found generated file as
/tmp/systemd-private-6a9ea6844b9c4c94883d23e4fb3e2215-apache2.service-shqCeo/tmp/box_rooms_1576324869.csv
That was very strange, as I do not know how read it from tmp path. Can it be done?
I think you need to pass Content-Type properly
$filePath= public_path(). "/upload/example.doc";
$headersContent = array('Content-Type: application/pdf',);
return Response::download($filePath, 'example.doc', $headersContent)

File storage not working. Laravel 5

I'm trying to save a .txt file in a folder named txts inside my public folder but the method i'm using is not working:
$txt_nombre = 'nomina_'. $extras['desde'] .'_'. $extras['hasta'] .'.txt';
$txt_contenido = '';
foreach ($personal as $per) {
$txt_cadena = 'BNC ' . $per->cuenta_nomina . ' ' . $per->netos . ' ' . $per->cedula_identidad;
$txt_contenido .= $txt_cadena . "\n";
}
Storage::put('public/txts/'. $txt_nombre, $txt_contenido);
It doesn't even throw an error, my function keeps running but the file is not being saved. Any idea why?
According to the Documentation
By default, the public disk uses the local driver and stores these files in storage/app/public.
Looking at your code, assuming that you haven't changed default configuration, your file should be created in storage/app/public/public/txts/ folder.
I am not sure how Laravel's Storage abstraction handles non-existing folders so you might want, for test, try using just Storage::put('file.txt', "content") and see if it's being created in storage/app/public folder

How can I upload image using Storage::put on the laravel?

My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps

Error while uploading in Laravel 4

I'm trying to make a image upload in lavarel 4. The upload passes by
The file "C:\xampp\tmp\phpD1F3.tmp" does not exist
This is my upload code :
$file = Input::file('image');
// Get extension
$extension =$file->getClientOriginalExtension();
// Generate a file name
$fileName = 'pool' . Str::quickRandom();
$fileNameExtension = $fileName . '.' . $extension;
// Process upload
Input::file('image')->move(public_path() . '/uploads/images/app/pools/original/', $fileNameExtension);
`
I don't know why i get this error , please can anyone help me to solve this?
You are probably making some operations on "uploaded" file after moving it to server by move() function. Please make sure there is no operation made on temporary file after calling Input::file('image')->move() function.

Locking specific cells with a password phpExcel

I am uploading an excel file from a folder in my computer to a folder in the server, after the upload i am loading the uploaded file so that i can protect certain cell, the first method i used below does not work at all
function LockCertainCells(){
$labref= $this->uri->segment(3);
$objReader = new PHPExcel_Reader_Excel2007();
$path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
$objPHPExcel = $objReader->load($path);
$objPHPExcel->setActiveSheetIndexbyName('Sample Summary');
$objPHPExcel->getActiveSheet()->protectCells('A17:G85','PHPExcel');
$objPHPExcel ->getActiveSheet()->getProtection()->setSheet(true);
}
This second one
function LockCertainCells(){
$labref= $this->uri->segment(3);
$objPHPExcel = new PHPExcel;
$path = "analyst_uploads/" . date('Y') . '/' . date('M') . '/'. $labref .'/'. $labref . ".xlsx";
$objSheet = $objPHPExcel->load($path);
$objSheet->setActiveSheetIndexbyName('Sample Summary');
$objSheet->protectCells('A17:G85', 'PHP');
$objSheet->getProtection()->setSheet(true);
}
Throws me this error:
Fatal error: Call to undefined method PHPExcel::load() in C:\127.0.0.1\htdocs\NQCL\.....
suggestions!
The PHPExcel class doesn't have a load method, which is precisely why you get that error.... the first method is the method provided by the PHPExcel library for loading a file into a PHPExcel object, the second is not. Use the method that works (and is the method described in all the documentation); not the one that doesn't exist.
The first method for locking the cells isn't doing anything with the PHPExcel object (such as saving it) after you have set cell protection; so as soon as the LockCertainCells function terminates, it will be out of scope and discarded by the PHP script. If you want to make the changes in the file, you need to save the file again. I believe that MS Excel itself requires you to save a file if you want to make any changes to that file permanent.
EDIT
You also miss setting the password for the protected cells:
$objSheet->getProtection()->setPassword('mypassword');

Resources