I'm uploading files to destinationPath and I'm having a problem with saving each filename when I'm trying to make a download link for those files.
foreach ($files as $file) {
$fileName = now()->format('Y-m-d-H-i-s');
$uploaded = Storage::put($destinationPath.$fileName.'.'.$file->getClientOriginalExtension(),file_get_contents($file->getRealPath()));
}
if($uploaded){
Project::create([
'description' => $request->input('description'),
'division' => $request->input('division'),
'who' => $request->input('who'),
'whom' => $request->input('whom'),
'content' => $request->input('content'),
'filename' => $division.'/'.$userName.'/'.$fileName
]);
}
Related
PLease help me on how to upload image in the folder and the same time in the database with a random name.
There's an error: Call to a member function getName() on null.
Heres my code in controller
`public function actionInsert()
{
$destination = new DestinationModel();
$name=$this->request->getVar('name');
$place=$this->request->getVar('place');
$location=$this->request->getVar('location');
$category=$this->request->getVar('category');
$description=$this->request->getVar('description');
$latitude=$this->request->getVar('latitude');
$longitude=$this->request->getVar('longitude');
$image=$this->request->getFile('image');
$imageName = $image->getName();
$image->move('im/destination', $imageName);
if($place == 'Calapan City')
{
$place = 'Calapan';
}else if($category == 'Destination')
{
$category ='Destination';
}
$data = [
'name' => $name,
'place' => $place,
'location' => $location,
'category' => $category,
'image' => $place. '/'. $imageName,
'description' => $description,
'latitude' => $latitude,
'longitude' => $longitude
];
$destination->save($data);
return view('adding_place');
}`
How can i upload a file and save the file using using original file name ?
Here my controller code ?
public function store(Request $request)
{
//Validate request data
$request->validate([
'name' => 'required',
'file' => 'required|mimes:pdf|max:50000',
'category' => 'required',
'year' => 'required|integer'
]);
$file = $request->file
$filename = Storage::disk('public')->putFile('regulations', $file);
Document::create([
'name' => $request->name,
'file' => $filename,
'category' => $request->category,
'year' => $request->year
]);
//redirect with success
return redirect()->back()->with('success', 'Done!');
}
Try this
$file = $request->file('file');
$file->getClientOriginalName();
I just want to save my image or file in the database as a URL that can easily get for the frontend. I tried it a lot but did not get a solution if anyone can get me out with this?
public function addPPQuestion(Request $pp_questions)
{
$validate = Validator::make($pp_questions->all(), [
'qual_cat_id' => 'required|exists:qualification_categories,qual_cat_id',
'year_id' => 'required|exists:years,year_id',
'course_id' => 'required|exists:courses,course_id',
'board_id' => 'required|exists:board_of__edus,board_id',
'paper' => 'required|mimes:pdf|max:2048',
]);
if ($validate->fails()) {
$messages = $validate->errors()->count() > 1 ? $validate->errors()->all() : $validate->errors()->first();
return response()->json(['code' => 400, 'message' => $messages], 400);
} else {
$paper = $pp_questions->paper;
$fileName = $paper->getClientOriginalName();
$pp_question = pp_question_answer::create([
'paper' => $pp_questions->paper->storeAs('', $fileName, ''),
'qual_cat_id' => $pp_questions->input('qual_cat_id'),
'year_id' => $pp_questions->input('year_id'),
'course_id' => $pp_questions->input('course_id'),
'board_id' => $pp_questions->input('board_id'),
]);
return response()->json(['code' => 201, 'message' => 'Question Created Successfully',
'object' => $pp_question], 201);
}
}
Step 1: Validate the files as you want in the api.
Content-Type: application/json
It will return JSON data as result.
$request->validate(['file' => 'required|mimes:jpeg,jpg,png,gif,svg,pdf,txt,doc,docx,application/octet-stream,audio/mpeg,mpga,mp3,wav|max:204800']);
Step 2: Make a file name & store it in the folder and get the URL of it.
$unique_id = strtolower(str_replace('.', '', uniqid('', true)) . time());
$photoFile = $request->file('paper');
$extension = $photoFile->getClientOriginalExtension();
$fileNameToStore = $unique_id . '.' . $extension;
$filepath = $photoFile->storeAs('photos', $fileNameToStore);
$pp_question = Model::create([
'paper' => $filepath,
'qual_cat_id' => $pp_questions->input('qual_cat_id'),
'year_id' => $pp_questions->input('year_id'),
'course_id' => $pp_questions->input('course_id'),
'board_id' => $pp_questions->input('board_id'),
]);
return response()->json([
'code' => 201,
'message' => 'Question Created Successfully',
'object' => $pp_question
], 201);
Step 3: To display data on the front-end side.
$imagePath = !empty($pp_question->pdf) && Storage::exists($pp_question->pdf) ? Storage::url($pp_question->pdf) : asset(Storage::url('default/pdf.png');
I'm trying to upload an image using the API. Even though, after running the first test, the database just stored the directory and not the file-name, worst part, it stored all directories that come before the one that is needed to store the image.
The code I'm using for the store is the following one:
public function store(Request $request)
{
$image = $request->file('image');
if($image == null){
$imagesDir = 'subjectImgs/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$fileDir = $images[array_rand($images)];
$path = $fileDir;
} else {
$path = $image->storeAs('uploads/images/store/', $image->getClientOriginalName(), 'public');
}
$subject = new Homework([
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$subject->save();
return response()->json([
"code" => 200,
"message" => "Homework added successfully"
]);
}
I'm uploading from a mobile device, I believe this info is also important (?) Same issue when in the simulator.
You are saving $path into $subject->image that is equal to public_path() . '/uploads/images/store/';. You should add $file->getClientOriginalName() if you want the filename :
$subject = new Homework([
...
'image' => $path . $file->getClientOriginalName(),
...
]);
You are settings 'image' => $path, and before that $path = public_path() . '/uploads/images/store/'; which yields the expected results.
Now if you want to only save from a form input, you can use the following code:
$image = $request->file('image');
$path = $image->storeAs('uploads/images/store/', $image->getClientOriginalName(), 'public');
Where 'public' is the default public filesystem as described in the doc here: https://laravel.com/docs/7.x/filesystem#the-public-disk
I'm new to laravel. I get the following error when uploading a file:
Call to a member function move() on null
$file = $request->file('img');
$destinationPath = base_path('\public\img');
$file->move($destinationPath . $file->getClientOriginalName());
$dealer = new Dealer([
'firstname' => $request->get('firstname'),
'lastname' => $request->get('lastname'),
'email' => $request->get('email'),
'phoneno' => $request->get('phoneno'),
'img' => $request->get('img'),
]);
Why dont You Try it like this ?
if ($request->hasFile('img')) {
$image = $request->file('img');
$teaser_image = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$image->move($destinationPath, $img);
} else {
dd('Request Has No File');
}
and For Your Store :
$dialer = Dialer::create([
'firstname' => $request->get('firstname'),
'lastname' => $request->get('lastname'),
'email' => $request->get('email'),
'phoneno' => $request->get('phoneno'),
'img' => $request->get('img') ?? null,
]);
You Can remove ??null for making sure that you get The image And store it in database but You can Even place It To make it Optional for the User To insert img or not . hope this helps
EDIT
According to your comment i guess you may have 2 problems :
first one be sure that you have and input that named 'img' that sends the image and the secound is that be sure to add the multi enctype to your form so that form can send image like below :
enctype="multipart/form-data"
so your form should be like this :
<form action="someRoute" method="post" enctype="multipart/form-data">
if ($request->hasFile('img')) {
$image = $request->file('img');
// print_r($image);
$image_name = time().'.'.$image->getClientOriginalExtension();
// echo $image;
// exit(0);
$destinationPath = base_path('Uploads');
$image->move($destinationPath, $image_name);
$dealer = new Dealer([
'firstname' => $request->get('firstname'),
'lastname' => $request->get('lastname'),
'email' => $request->get('email'),
'phoneno' => $request->get('phoneno'),
'img' => $image_name,
]);
$dealer->save();
Session::flash('msg','Data Added successfully');
Session::flash('type','success');
return redirect('dealer-master');
// // echo $image;
// // exit(0);
// $destinationPath = base_path(' Uploads');
// $image->move($destinationPath, $image_name);
}
else {
Session::flash('msg','Please Check the data');
Session::flash('type','fail');
return redirect('dealer-master');
// echo $request;
}
I Findout my mistake This is working good Thank U Guys...!
It's work form me
if($request->img){
$fileName = time() . '.' . $request->img->extension();
$request->img->move(storage_path('app/public/img'), $fileName);
}
$dealer = new Dealer([
'firstname' => $request->get('firstname'),
'lastname' => $request->get('lastname'),
'email' => $request->get('email'),
'phoneno' => $request->get('phoneno'),
'img' => $fileName ?? null,
]);
$dealer->save();