G
G
Givi7772019-09-18 17:25:20
Laravel
Givi777, 2019-09-18 17:25:20

How to upload images to Laravel server?

if ($request['files']){
            $files = $request->file('files');

            foreach ($files as $file) {
                $path = $file->store('uploads/company');
                $disk->setVisibility($path, 'local');
                
                $created_photo = CompanyFile::query()->create([
                    'company_id' => $id,
                    'file_type' => $path,
                ]);
            }
        }

images with paths are stored in the database and not uploaded to the server?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LaraLover, 2019-09-18
@LaraLover

Try instead $path = $file->store('uploads/company'); use move

$filename = $this->getFileName($request->file);
    	$request->image->move(base_path('uploads/company'), $filename);

N
Nikolay Alekseev, 2019-09-18
@VariusRain

Function code that I use in my projects.
It implies loading using AJAX, then this does not change the essence of the matter

public function uploadGallery(Request $req) {
  try {
    $files = $req->file('photo');
    foreach ($files as $file) {
      $tmp1 = md5(microtime());
      $tmp2 = md5(uniqid());
      $newName = strtolower(substr(base64_encode($tmp1 . $tmp2), 0, 7)).'.'.$file->getClientOriginalExtension();
//здесь важная поправка куда перемещать - это абсолютный путь
//так что к слову media в моём случае нужно ещё применить функцию base_path или конкотинировать результат функции public_path
      $file->move('media', $newName);
      //Storage::putFileAs('/public/ //строчка закомментирована, но можно использовать её
    }
  } catch (\Exception $err) {
    //$errorcode = $err->getMessage();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question