FileUploadController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\FileUpload;
  7. use File;
  8. class FileUploadController extends Controller
  9. {
  10. public function index()
  11. {
  12. $data['title'] = "Upload File";
  13. $data['all'] = FileUpload::all();
  14. return view('admin.file_upload',$data);
  15. }
  16. public function create()
  17. {
  18. //
  19. }
  20. public function store(Request $req)
  21. {
  22. $file = $req->file('file');
  23. $file_name = $req->name;
  24. $details = $req->details;
  25. if ($req->hasFile('file')) {
  26. $extension = $file->getClientOriginalExtension();
  27. $os = array("zip", "rar", "pdf", "doc", "docx", "txt");
  28. if (in_array($extension, $os)){
  29. $name = $file->getClientOriginalName();
  30. $newFileName = $newFileName = "file_".date('d_m_y_h_m_s').".".$extension;
  31. $fileee = $file->move('assets/document/files/', $newFileName);
  32. $file_name = empty($file_name)?$name:$file_name;
  33. $cat = new FileUpload;
  34. $cat->url = $newFileName;
  35. $cat->details = $details;
  36. $cat->name = $file_name;
  37. $cat->save();
  38. }
  39. }
  40. return redirect()->back();
  41. }
  42. public function show($id) //use as delete
  43. {
  44. $del = FileUpload::find($id);
  45. if (File::exists('assets/document/files/'.$del->url) && !empty($del->url))
  46. {
  47. File::delete('assets/document/files/'.$del->url);
  48. $del->delete();
  49. }
  50. return redirect()->back();
  51. }
  52. public function edit($id) //use for download
  53. {
  54. }
  55. public function update(Request $request, $id)
  56. {
  57. //
  58. }
  59. public function destroy($id)
  60. {
  61. //
  62. }
  63. }