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.
Related
I have the following code:
public function store(string $folder, $file){
$ret = Storage::putFileAs("/application/public/storage/1/{$folder}", $file, $file->getClientOriginalName());
return $ret;
}
}
Where the path to the putFileAs function ends up being /application/public/storage/1/tasks/NXL-1 after feeding the variable $folder, but even tho the variable $ret in the return has the path to that file as if it worked /application/public/storage/1/tasks/NXL-1/image.png, when I go to that folder the file is not there. What should I do?
you can use storeAs method in laravel 8.
for example
$imageFileName = $request->image_name->getClientOriginalname();
$request->image_name->storeAs('images/portfolio', $imageFileName, 'publicfolder');
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 am studying some laravel code that I downloaded and I am getting some problem.
This supposed to be the functions to save,delete and download the files but the problem is.
The files are being saved in a folder named with a number on "storage\app\public\project-files\" (i.e. storage\app\public\project-files\11), both destroy and download methods are referencing different paths, I tried to change but didn't worked, download show FileNotFoundException and destroy just remove from the database but not from the folder
So is this code wrong? How It supposed to be?
I've read about using artisan:link but seems odd to me run this command every time I want upload a file to make a link
PS. I cheched the routes, so the methods are being called
Thanks
public function store(Request $request)
{
if ($request->hasFile('file')) {
$file = new ProjectFile();
$file->user_id = $this->user->id;
$file->project_id = $request->project_id;
$request->file->store('public/project-files/'.$request->project_id);
$file->filename = $request->file->getClientOriginalName();
$file->hashname = $request->file->hashName();
$file->size = $request->file->getSize();
$file->save();
$this->project = Project::find($request->project_id);
return view('project-files');
}
public function destroy($id)
{
$file = ProjectFile::find($id);
File::delete('storage/project-files/'.$file->project_id.'/'.$file->hashname);
ProjectFile::destroy($id);
$this->project = Project::find($file->project_id);
return view('project-files');
}
public function download($id) {
$file = ProjectFile::find($id);
return response()->download('storage/project-files/'.$file->project_id.'/'.$file->hashname);
}
You are storing files in storage so i assume you have uploaded image in the following path
project\storage\app\public\project-files
if this is the path then you can delete using
Storage::delete('public/project-files/1.JPG');
for Downlaoding file
$path= storage_path('app/public/project-files/3.JPG');
return response()->download($path);
I want to upload a file in Laravel:
I put this code in route
Route::post('upload', 'myconttest#test')->name('upload');
And put this code in controller
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
// $projectname;
$path='files/';
$file->move($path,$filename);
}
I create a folder in public called files. The code runs without error but the file is not saved.
Its a working code please see this
public function store(Request $request)
{
$checkval = implode(',', $request->category);
$input = $request->all();
$input['category'] = $checkval;
if ($request->hasFile('userpic')) {
$userpic = $input['pic'];
$file_path = public_path("avatars/$userpic");
if(File::exists($file_path)) {
File::delete($file_path);
}
$fileName = time().$request->userpic->getClientOriginalName();
$request->userpic->move(public_path('avatars'), $fileName);
$input['userpic'] = $fileName;
Product::create($input);
return redirect()->route('productCRUD.index')->with('success','Product created successfully');
}
}
Make sure your form is enabled to upload the file. So check that the following attribute is set or not.
<form action"" 'files' => true>
Use the following namespace
use Illuminate\Support\Facades\Input;
Then try this:
$file = Input::file('myfile');
$filename = $file->getClinetOriginalName();
move_uploaded_file($file->getPathName(), 'your_target_path/'.$filename);
I think you can try this:
First you give folder permission to files folder in public folder
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
$file->move(public_path().'/files/', $filename);
}
Hope this work for you !!!
In controller test function you need to given public path of files folder like
$path = public_path('files/');
for moving file on given public folder path.
I would like to write some data (from a custom controller plugin) into files under the 'messages' subdirectory of my website's public directory.
The custom controller plugin is quite simple:
<?php
namespace Main\Service;
class MessageWriter
{
public $data = array();
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function write_message()
{
$this->time = time();
$data = serialize($this->data);
$filename = time() . '_' . rand(1000, 9999);
#file_put_contents('/var/www/public/messages/' . $filename, $data);
}
}
?>
I have two tiny problems / questions:
How can i avoid the hardcoding of the path? Isn't there a ZF2 config variable which holds the real path of the public directory (/var/www/public).
Is there any class for writing files in ZF2, or it's OK using file_put_contents and similar file functions?
Thank you very much!
Mike
In ZF2, all paths are relative to the application root, so you should be able to use file_put_contents('public/messages/' . $filename, $data). And file_put_contents() is fine to use.
Try...
APPLICATION_PATH.'/../public/messages/'.$filename
Edit: just realised this is zf2 not zf1...
Everything is relative to the application path in ZF2