我正在尝试使用公共创建的路径(即上载/图像)将图像上载到数据库,但是不幸的是,它没有显示任何错误,并且图像没有上载或保存到目录中。
控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Image;
class ImagesController extends Controller
{
public function index()
{
return view('image');
}
public function upload(Request $request)
{
$this->validate($request, [
'images' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$image = $request->file('images');
$input['imagename'] = time() . '.' . $image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/images');
$img = Image::make($image->getRealPath());
$img->resize(100, 100, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath . '/' . $input['imagename']);
$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);
if (!empty($input['imagename'])) {
return response()->json([
'success' => true,
'data' => $input['imagename']->toArray()
]);
}
return true;
}
}
路线
Route::post('upload', 'ImagesController@upload');
模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $fillable = ['image'];
}
答案 0 :(得分:1)
在将图像文件保存到目录之前,请确保已在<form>
标记中添加了 enctype =“ multipart / form-data” 。
检查一下,您是否已从位于 C:\ xampp \ htdocs \ isp \ config \ filesystems.php 的文件系统更改了公共路径。 现在检查我的以下代码以了解想法并根据此代码修改您的代码。
public function site(Request $request)
{
if ($request->hasFile('imagename')) {
$image = $request->file('imagename');
$filename = 'image'.rand(1,5) . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/image/' . $filename);
Image::make($image)->save($location);
}
Session::flash('flash_message_success', 'Site profile updated successfully');
return redirect()->route('site');
}
我认为您在 public_path()函数中提供了额外的 / 。改正它。并更改您的验证参数'images' => 'required|image|mimes:jpeg,png,jpg,gif,svg,JPEG,PNG,JPG,GIF,SVG|max:2048',
希望任何条件都能得到满足,您会成功
答案 1 :(得分:1)
尝试采用这种样式,然后运行
php artisan storage:link
此外,将您的控制器更改为此
public function upload(Request $request)
{
$img = new Image();
$validation = $request->validate([
'title' => 'string',
'image' => 'required|file|image|mimes:jpeg,png,gif,webp|max:2048'
]);
$file = $validation['image']; // get the validated file
$extension = $file->getClientOriginalExtension();
$filename = 'mm-image-' . time() . '.' . $extension;
$path = $file->storeAs('/uploads/images', $filename);
$img->image = $request->image;
$img->title = $path;
dd($img);
if (!empty($img)){
return response()([
'success' => true,
'data' => $img->toArray()
]);;
}
}