HelperController.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class HelperController extends Controller
  5. {
  6. public function upload_single_photo ($photo, $type)
  7. {
  8. $rand = rand(0,999);
  9. $name = time().$rand.'.'.$type;
  10. // file_put_contents(public_path().'/uploads/'.$name, file_get_contents($photo));
  11. return $link = 'uploads/'.$name;
  12. }
  13. public function csvToArray ($filename = '', $delimiter = ',')
  14. {
  15. if (!file_exists($filename) || !is_readable($filename))
  16. return false;
  17. $header = null;
  18. $data = array();
  19. if (($handle = fopen($filename, 'r')) !== false)
  20. {
  21. while (($row = fgetcsv($handle, 1000, $delimiter)) !== false)
  22. {
  23. if (!$header)
  24. $header = $row;
  25. else
  26. $data[] = array_combine($header, $row);
  27. }
  28. fclose($handle);
  29. }
  30. return ['users' => $data] ;
  31. }
  32. public function display_duplicated_emails ($emails)
  33. {
  34. $duplicates = array_diff_key($emails, array_unique($emails));
  35. if(!empty($duplicates)){
  36. $error = [];
  37. foreach ($duplicates as $key => $duplicate) {
  38. $error[] = $duplicate ." has already duplicate in json";
  39. }
  40. return response()->json(compact('error'),400);
  41. }
  42. }
  43. } //class