2014-12-01 2 views
1

CSV Excel 파일의 데이터를 데이터베이스에 삽입하려고하지만 코드가 작동하지 않는 것처럼 보입니다. 어디서 잘못되었는지 알 수 없습니다. 내가 어떤 데이터가 삽입되지 않고 내가 어떤 오류가 발생하지 않습니다 위의 코드를 실행하면데이터를 CSV 파일에서 데이터베이스에 삽입합니다.

public function postUploadS(){ 

    $file = array('file' => Input::file('file')); 

    $rule = array('file' => 'required'); 
    $mime = array( 'text/csv', 
        'text/plain', 
        'application/csv', 
        'text/comma-separated-values', 
        'application/excel', 
        'application/vnd.ms-excel', 
        'application/vnd.msexcel' 
       ); 
    $uploaded_mime = Input::file('file')->getMimeType(); 



    $staff_list=$_FILES['file']['tmp_name']; 

    $linecount = ""; 
    $cols = 5; 
    $numx =""; 
    $mimes = array(
        'text/csv', 
        'application/csv', 
        'text/comma-separated-values', 
        'application/excel', 
        'application/vnd.ms-excel', 
        'application/vnd.msexcel',); 

     if(empty($staff_list)){ 
       $_SESSION['error']="<font color='#FF0000'>Please choose the csv file to upload</font>"; 
       } 

     elseif(!in_array($_FILES['file']['type'], $mimes)) { 
       $_SESSION['error']="<font color='#FF0000'>Invalid file format, Please choose only csv exel format</font>"; 

       } 

     else{ 
      $staff_list=$_FILES['file']['tmp_name']; 

      $handle=fopen($staff_list,"r"); 

      // read the first line and ignore it 
      fgets($handle); 

      //count number of lines of uploaded csv file 

      $fh = fopen($staff_list,'rb') or die("ERROR OPENING DATA"); 
        while (fgets($fh) !== false) $linecount++; 
         fclose($fh); 


      var_dump($fh); exit(); 

      while(($fileop=fgetcsv($handle,1000,",")) !==false){ 


       $fullname = $fileop[1]; 
       $staff_id = $fileop[0]; 
       $gender = $fileop[2]; 
       $position= $fileop[3]; 
       $department= $fileop[4]; 
       $numx = count($fileop); 

       $tfulname = trim($fullname); 
       $lfulname = strtolower($tfulname); 

       $name_full = explode(' ', $lfulname); 

       $firstname = $name_full[0]; 
       $middlename = implode(array_slice($name_full, 1, -1)); 
       $lastname = end($name_full); 
       $phone = ''; 
       $email = ''; 
       $college = ''; 

       if($gender == 'M'){ 
        $gender == 'Male'; 

       } 
       elseif ($gender =='F') { 
        $gender == 'Female'; 
       } 


     DB::beginTransaction(); 

    try{ 
     $staff = new Staff; 
     $staff->staff_id = $staff_id; 
     $staff->firstname = $firstname; 
     $staff->middlename = $middlename; 
     $staff->lastname = $lastname; 
     $staff->gender = $gender; 
     $staff->position = $position; 
     $staff->phone = $phone; 
     $staff->email = $email; 
     $staff->college = $college; 
     $staff->department = $department; 
     $staff->save(); 

    } 
    catch(Exception $e){ 

     Session::put('key','There is a duplicate row in your data,check the file and try again'); 
    } 

     $cPass = ucfirst($lastname);  
     $hashed_pass = Hash::make($cPass); 

     $user = new User; 
     $user->username = $staff_id; 
     $user->password = $hashed_pass; 
     $user->save(); 


     } 


     if($numx!=$cols){ 
       DB::rollback(); 
        Session::put("error","<font color='#FF0000'>Error,number of columns does not match the defined value,please check your file and try again</font>"); 
         } 

     elseif(Session::has('key')){ 
        DB::rollback(); 
        Session::put("error","<font color='#FF0000'>Error,duplicate entry detected in your file,please check your file and try again</font>"); 
        } 

     else{ 

       DB::commit(); 
       Session::put("success","<font color='#0099FF'>Staff list has been uploaded successfully</font>"); 

          } 

} }

:

여기 내 코드입니다. 당신은 당신이 그것을 시도하지 않았다 Laravel 당신이 "$ csv-> fetchAll()"와 같은 - 그런 사용할 수 있습니다 사용하는 경우

+0

을 기쁘게 도와 드릴까요? – IshaS

답변

2
$file = Reader::createFromPath('path-to-your-file'); 

$result = $file->fetchAll(); 

foreach($result as $data){ 
// do database add here 
} 
관련 문제