Guest User

Untitled

a guest
Mar 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public function upload(RequestsCreatePostsRequest $request)
  2. {
  3. $target_dir = "uploads/";
  4. $target_file = $target_dir . basename($_FILES['fileToUpload']["name"]);
  5. $uploadOk = 1;
  6. $findme = ".";
  7. $pos = strpos($target_file, $findme);
  8. //echo $target_file;
  9. //dd($_POST);
  10. echo ini_get('upload_max_filesize');
  11. echo $_FILES["fileToUpload"]["error"];
  12. $imageFileType = strtolower(substr($target_file,$pos+1));
  13. if(isset($_POST["submit"])) {
  14. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  15. //$check = true;
  16. //dd($check);
  17. if($check !== false) {
  18. echo "File is an image - " . $check["mime"] . ".";
  19. $uploadOk = 1;
  20. } else {
  21. echo "File is not an image.";
  22. $uploadOk = 0;
  23. }
  24. }
  25. $filecheck = 0;
  26. $orgFileName = $target_file;
  27. while (file_exists($target_file)) {
  28. $target_file = substr($orgFileName,0,$pos).$filecheck.substr($orgFileName,$pos);
  29. $filecheck++;
  30.  
  31. $uploadOk = 1;
  32. }
  33.  
  34. if ($_FILES["fileToUpload"]["size"] > 500000000*8) {
  35. echo "Sorry, your file is too large.";
  36. $uploadOk = 0;
  37. }
  38.  
  39. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  40. && $imageFileType != "gif" ) {
  41. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  42. echo $imageFileType;
  43. $uploadOk = 0;
  44. }
  45.  
  46. if ($uploadOk == 0) {
  47. echo "Sorry, your file was not uploaded.";
  48.  
  49. } else {
  50. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  51. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  52.  
  53. $post = new Posts($request->all());
  54. $post->fileToUpload = $target_file;
  55. Auth::user()->posts()->save($post);
  56. } else {
  57. echo "Sorry, there was an error uploading your file.";
  58. }
  59. }
  60. return redirect('');
  61. echo $target_file;
  62. }
  63.  
  64. $target_file = $target_dir . preg_replace("/[^A-Za-z0-9_-.]/", '', basename($_FILES['fileToUpload']["name"]));
Add Comment
Please, Sign In to add comment