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);
}
Related
I'm trying to upload multiple pdf files using Laravel. I've made the form using simple HTML and enabled the multiple attribute in the input field. I've tried many other methods mentioned in the stack community but they didn't work for me.
In the controller, I've the following to check whether the files are being uploaded or not. The foreach loop will return the path of the files uploaded.
Controller Code
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path = $attachment->store('corporate');
dd($path);
}
HTML Form Code
<input type="file" name='corpfile[]' accept=".xls, .xlsx, .xlsm, .pdf" multiple>
The dd($file) is returning an array with all the files uploaded but the dd($path) is returning only one file's path.
I don't know what the issue is. Any help is appreciated.
you're diying the loop with dd
if you want to store the path in array , you should do that
$path = [];
if ($request->hasFile('corpfile')) {
$file = $request->file('corpfile');
// dd($file);
foreach ($file as $attachment) {
$path[] = $attachment->store('corporate');
}
}
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.
Hi need help in creating nested folders.
I currently have a folder called images and i would like to clone the folders and save it in a different folder call backup. and the backup will only happen when user clicks backup.
For example:
- Images
- Car
- A
- A1
- A1-1
- A2
- B
- Van
How can i write the code so that it will create the folders?
Currently i have done this so how can i do it?
public function sync(Request $request)
{
$arr = [];
$folderToSync = $request->input('folderName');
$originalPath = public_path($folderToSync);
$newFolderPath = public_path('S3/'.$folderToSync);
$this->createFolder($newFolderPath); // create the selected folder
$directories = $this->getAllFolders($originalPath); // getting all folders under the original path
$this->folder($directories, $newFolderPath, $originalPath);
dd('end');
}
public function createFolder($path)
{
if (!is_dir($path)) {
#mkdir($path, 0777, true);
}
}
public function folder($directories, $newFolderPath, $originalPath)
{
foreach ($directories as $directory) {
$newPath = $newFolderPath.'/'.$directory;
$oriPath = $originalPath.'/'.$directory;
$this->createFolder($newPath);
$subFolders = $this->getAllFolders($oriPath);
if ($subFolders) {
$this->subfolder($subFolders, $newPath);
}
}
}
public function subfolder($directories, $path)
{
foreach ($directories as $directory) {
$this->createFolder($path.'/'.$directory);
}
}
public function getAllFolders($path)
{
return array_map('basename', \File::directories($path));
}
public function getAllFiles($path)
{
return;
}
but its not creating the subfolders. How can i modify it ?
i would run the code every week and i want to check also which folder have been created and which have not been created. if the folder does not exist then create the folder.
I would have a look at the storage api from the Laravel documentation: https://laravel.com/docs/5.7/filesystem#directories
To acquire folders and subfolders:
// Recursive...
$directories = Storage::allDirectories($directory);
Creating a new directory:
Storage::makeDirectory($directory);
Storing files:
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $resource);
The put method will take either the file contents or a resource.
Don't forget to include the Storage facade:
use Illuminate\Support\Facades\Storage;
I need to list files from storage->app->public->huetten->it contains 17 folders. Each folder contains images. Any help would be appreciated.
public function show($id)
{
if(!empty($id)) {
$directories = Storage::disk('public')->allDirectories('huetten');
foreach ($directories as $directory) {
//dd($directory); // Here i get huetten/folder1
$files = Storage::files($directory);
foreach ($files as $file) {
dd($file); // But here files are not listing
}
}
}
}
Storage uses the default Filesystem disk in config/filesystems.php which is set to local (i.e storage/app/) in a fresh Laravel project.
I assume that it is set to this same value in your project.
You want to list files in storage/app/public, and this corresponds to the public disk in config/filesystems.php.
In your loop, you can correctly the search directory by getting a Filesystem instance that for the public disk like you already do when listing the directories.
$files = Storage::disk('public')->files($directory);
/* [ 'storage/app/public/huetten/folder1/foo', 'storage/app/public/huetten/folder1/bar', ...] */
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.