I have this function that export the csv, the it outputs the file so the user can download it.
public function export()
{
$people = Person::all();
$csv = \League\Csv\Writer::createFromFileObject(new \SplTempFileObject());
$csv->insertOne(\Schema::getColumnListing('people'));
foreach ($people as $person) {
$csv->insertOne($person->toArray());
}
$csv->output('people.csv');
}
Instead of this I want to save the file in a project folder.
how could i do it?
thanks
Don´t use createFromFileObject, instead
$csv = \League\Csv\Writer::createFromPath('path/to/people.csv', 'w');
if the file doesn´t exist, this will create.
And remove $csv->output('people.csv'), it´s not necessary.
Related
I have a form input which can upload multiple files. So all the files paths are saved in the database as an array of strings . Below is my controller to download the files. I wanted to know how I can go about presenting each file to be downloaded in blade view. So I could download a single file at a time. My controller below. It works only when downloading a single file. I get this error Invalid argument supplied for foreach().
public function download($id) {
$deal = Deal::findorFail($id);
$files = $deal->uploads;
foreach ($files as $file) {
return Storage::download($file);
}
}
public function download($id) {
$deal = Deal::findorFail($id);
$files = $deal->uploads;
foreach ($files as $file) {
return Storage::download($file);
--------^^^^^^-------------------------
}
}
when you return first file, it breaks foreach loop. So you have to return all files together. And only way to achieve this, is creating a zip file that contains all files..
For this purpose you may use chumper/zipper package
$zipper = Zipper::make(public_path('/documents/deals.zip'));
foreach ($files as $file) {
$zipper->add(public_path($file)); // update it by your path
}
$zipper->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);
update
Add accessor to Deal model to get files as array
Deal Model
php artisan getUploadsAttribute($attribute){
return explode(",",$attributes);
}
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.
public function uploadNotaCorte(Request $request, EstadoRepository $estadoRepository)
{
$error = array();
$path = $request->file('file')->getRealPath();
$notasCorte = Excel::load($path, function($reader) {
})->get();
$chunk = $notasCorte->chunk(100);
foreach ($notasCorte as $key => $notaCorte) {
//RULES
}return $error;
}
**
Hi everyone, I'm new to programming and I'm having a hard time implementing the chunk method, so the dodigo above usually works on small files plus larger error files because of the size.
I need to upload a file with 120,000 records and I am trying to use the chunk for this, I do not know what I can do wrong already looked at the documentation more and very simple and I could not solve the problem can anyone help me ??**
Assuming you're using the maatwebsite/excel package, this link should help: http://www.maatwebsite.nl/laravel-excel/docs/import#chunk
You'll want to change your code to something like this:
public function uploadNotaCorte(Request $request, EstadoRepository $estadoRepository)
{
$error = array();
$path = $request->file('file')->getRealPath();
Excel::filter('chunk')->load($path)->chunk(100, function($results)
{
foreach($results as $row)
{
// RULES
}
});
return $error;
}
This isn't tested and I've never used that package (though good to know it exists) so your mileage may vary.
I have a CSV file named beatles.csv that has the following contents on each cell:
John,Paul,George,Ringo
In vanilla PHP, this code works without issue (CSV file is in the same location as the script):
$data = 'beatles.csv';
$file = new SplFileObject($data);
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
echo $row[0].'<br>'; // outputs John
}
In Laravel 5.4, I created a folder named MyDomainName under the app folder and I put the CSV file there so its namespaced as follows:
use App\MyDomainName;
And the content of my method on the controller which should fetch the CSV data is as follow:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\MyDomainName;
class PageController extends Controller
{
public function fetchCSV()
{
$file = new SplFileObject('beatles.csv');
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row){
dd($row[0]);
}
}
}
And I get this issue:
FatalThrowableError in PageController.php line 67:
Class 'App\Http\Controllers\SplFileObject' not found
I thought that by using use App\MyDomainName; it will solve the issue but still it didn't solve it.
I have also explicitly use the full path but still it didn't work.
$file = new \App\MyDomainName\SplFileObject('App\MyDomainName\beatles.csv');
and I still get the same error:
FatalThrowableError in PageController.php line 67:
Class 'App\MyDomainName\SplFileObject' not found
What could be the issue why is it that the SplFileObject class cannot be found?
In your fetchCSV method replace this code
$file = new \SplFileObject('beatles.csv');
I've faced the same use. You just need to add the forward-slash before SplFileObject()
thanks #Mark Baker, the issue is now solved:
public function fetchCSV()
{
$file = new \SplFileObject('../App/MyDomainName/beatles.csv');
$file->setFlags(\SplFileObject::READ_CSV);
foreach ($file as $row){
dd($row[0]); // outputs "John"
}
}
I am reading two excel file, using php-excel-reader (From this)
After reading two files of 1st row, I am comparing it. If they are same then I am appending contain of on file to other. To write the file I am using this
Now for doing this I want to close one file, but that function is not available in php-excel-reader
here is my code
compare file
{
$data = new Spreadsheet_Excel_Reader($filepath);
$data1 = new Spreadsheet_Excel_Reader($destinationfilepath);
}
unset($data);
unset($data1);
if($flag==0)
{
$excel = new ExcelWriter($destinationfilepath);
// read the source file
$finalarray= array();
for($m=1;$m<$sourcefilerowcount;$m++)
{
$charvalue='A';
$temprow=$m+1;
for($n=0;$n<$destinationcolnum;$n++)
{
$data = new Spreadsheet_Excel_Reader($filepath);
$finalarray[$n]=$data->val($temprow,$charvalue);
$charvalue++;
}
print_r($finalarray)."<br/>";
$excel->writeLine($finalarray);
}
There is no need to explicitly call close() function, because the file is automatically closed in the load() method. If you look at Excel2007.php, where PHPExcel_Reader_Excel2007 is defined, you'll see:
public function load($pFilename)
{
...
$zip = new ZipArchive;
$zip->open($pFilename);
...
$zip->close();
return $excel;
}
Just unset your PHPExcel_Reader object, and the data will be removed from memory:
$objReader = PHPExcel_IOFactory::createReader('Excel2003XML');
$objPHPExcel = $objReader->load("Excel2003XMLTest.xml");
...
unset($objPHPExcel);
unset($objReader);