Answer the question
In order to leave comments, you need to log in
Why is the incorrect path of the Lavel files recorded?
When writing to the database, this file path is shoved
D:\OSPanel\userdata\temp\php286.tmp
public function update(Request $request, Product $product)
{
$file = Input::file('image');
$destinationPath = public_path(). '/uploads/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, rand(1,9999999).$filename);
$file_path = '/public/uploads/'.$filename;
$request->image = $file_path;
$product->categories()->detach();
if ($request->input('categories')) {
$product->categories()->attach($request->input('categories'));
}
return redirect()->route('admin.product.index');
}
Answer the question
In order to leave comments, you need to log in
1. I don't see the path to the file in the database in this code at all. Most likely you are trying to do it here $request->image = $file_path;
. I roughly understand why you are doing this, but I would advise you to abandon such tricks with respect to base classes (if this is not described in the documentation). But in any case, further down the code there is only the use of the field value categories
, but there is no picture. Most likely you are hoping for some kind of observer and in it you again access request
, but there will not be the changes that you made to it, since you only changed your local variable.
2. Try to avoid using facades ( Input::file('image')
), they are slow. And it's not clear why you are doing this when you have a variable $request
3. Do not userand
to create unique filenames. The chance of coincidence, though extremely small, but it is. It's better to create a normal file naming method once than to do it the way it is now
4. Don't store all downloaded files in one folder. When the number of files exceeds a thousand, there will be noticeable brakes when accessing this folder
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question