public function uploadFiles(Request $request) { $destinationPath = "media"; $files = $request->file("myFiles"); $uploadCount = 0; $lastFile = MediaStorage::orderBy('id', 'Desc')->take(1)->get()->toArray(); $fileCount = count($files); foreach ($files as $file) { $uploadCount++; if (count($lastFile) === 0) { $fileName = strval(count($lastFile) + 1) . "_" . $file->getClientOriginalName(); $uploadSuccess = $file->move($destinationPath, $fileName); MediaStorage::create(["filename" => $file->getClientOriginalName(), "filesize" => $file->getSize(), "filelocation" => "/" . $destinationPath . "/" . $fileName]); $lastFile = MediaStorage::orderBy('id', 'Desc')->take(1)->get()->toArray(); } else { $fileName = ++$lastFile[0]['id'] . "_" . $file->getClientOriginalName(); $uploadSuccess = $file->move($destinationPath, $fileName); MediaStorage::create(["filename" => $file->getClientOriginalName(), "filesize" => $file->getSize(), "filelocation" => "/" . $destinationPath . "/" . $fileName]); } } return view("successful-upload", compact('uploadCount')); }
public function uploadContents(Request $request) { $destinationPath = "uploads"; $files = $request->file("myFiles"); $uploadCount = 0; $lastFile = MediaStorage::orderBy('id', 'Desc')->take(1)->get()->toArray(); $fileCount = count($files); foreach ($files as $file) { $uploadCount++; if (count($lastFile) === 0) { $fileName = strval(count($lastFile) + 1) . "_" . $file->getClientOriginalName(); $uploadSuccess = $file->move($destinationPath, $fileName); MediaStorage::create(["filename" => $file->getClientOriginalName(), "filesize" => $file->getSize(), "filelocation" => "/" . $destinationPath . "/" . $fileName, "ip" => $this->getIP(), "isDeleted" => false]); $lastFile = MediaStorage::orderBy('id', 'Desc')->take(1)->get()->toArray(); } else { $fileName = ++$lastFile[0]['id'] . "_" . $file->getClientOriginalName(); $uploadSuccess = $file->move($destinationPath, $fileName); MediaStorage::create(["filename" => $file->getClientOriginalName(), "filesize" => $file->getSize(), "filelocation" => "/" . $destinationPath . "/" . $fileName, "ip" => $this->getIP(), "isDeleted" => false]); } } return view('uploadSuccessful', compact('uploadCount')); //should fix something with redirect with var }