2015-12-15 62 views
0

我有一個核心PHP腳本,它允許我上傳文本文件並將其數據導入到sql數據庫中。如何使用Codeigniter上傳文本文件並在sql中導入其數據?

我想用Codeigniter做同樣的事情。我試着在屏幕上顯示我空白的白色屏幕,當我把代碼放在CI應用程序中。

下面是上傳文件和導入是我的核心PHP腳本:

<?php 
include_once 'dbconfig.php'; 
if(isset($_POST['btn-upload'])) 
{  

    $file = rand(1000,100000)."-".$_FILES['file']['name']; 
    $file_loc = $_FILES['file']['tmp_name']; 
    $file_size = $_FILES['file']['size']; 
    $file_type = $_FILES['file']['type']; 
    $folder="uploads/"; 
    $location =$_FILES['file']; 

    // new file size in KB 
    $new_size = $file_size/1024; 
    // new file size in KB 

    // make file name in lower case 
    $new_file_name = strtolower($file); 
    // make file name in lower case 

    $final_file=str_replace(' ','-',$new_file_name); 

     if(move_uploaded_file($file_loc,$folder.$final_file)) 
    { 
     $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')"; 
     mysqli_query($connection,$sql); 


     $handle = fopen("c:/wamp/www/codeigniter/uploads/$file", "r"); 

     if ($handle) { 
       while (($line = fgets($handle)) !== false) { 

        $lineArr = explode("\t", "$line"); 
        //var_dump($lineArr); // to make sure array is ok 

        // instead assigning one by onb use php list -> http://php.net/manual/en/function.list.php    
        list($emp_id, $date_data, $abc, $def, $entry, $ghi) = $lineArr; 

        // and then insert data 
        mysqli_query($connection,"INSERT INTO `daily_data2` (emp_id, date_data, abc, def, entry, ghi) 
      VALUES ('$emp_id', '$date_data', '$abc', '$def', '$entry', '$ghi')"); 
       } 

       fclose($handle); 
      } 


     ?> 
     <script> 
     alert('successfully uploaded'); 
     window.location.href='index.php?success'; 
     </script> 
     <?php 


    } 
    else 
    { 
     ?> 
     <script> 
     alert('error while uploading file'); 
     window.location.href='index.php?fail'; 
     </script> 
     <?php 
    } 

} 

?> 

我不知道如何在笨做到這一點。請指導我在這裏

詞表:

 <h2>Upload CSV To Import Users</h2> 
     <form method="post" action="<?php echo site_url('admin/attendance/upload');?>" enctype="multipart/form-data"> 
      <input type="file" name="userfile" ><br><br> 
      <input type="submit" name="submit" value="UPLOAD" class="btn btn-primary"> 
     </form> 
+0

爲什麼你不使用笨數據庫的方法來此? –

+0

我想爲此使用codeigniter方法,但我不知道如何做到@Abdulla – Rajan

+0

@Abdulla我試圖在CI中做同樣的事情,但失敗 – Rajan

回答

1

在控制器

## include_once 'dbconfig.php'; 
## Use defult Databse connection 

if(isset($_POST['btn-upload'])) 
{  

    $file = rand(1000,100000)."-".$_FILES['file']['name']; 
    $file_loc = $_FILES['file']['tmp_name']; 
    $file_size = $_FILES['file']['size']; 
    $file_type = $_FILES['file']['type']; 
    $folder="uploads/"; 
    $location =$_FILES['file']; 

    $new_size = $file_size/1024; 
    $new_file_name = strtolower($file); 
    $final_file=str_replace(' ','-',$new_file_name); 
    $new_name = $folder.$final_file; 

    if(!move_uploaded_file($final_file,$new_name)) # Change This 
      echo "Error in Upload"; 
     } 
     else{ 

      $file_uploded = base_url().'uploads/'.$file; 

      if (!file_exists($file_uploded)) { 
       echo "File Not exist"; 
      } 
      else 
      { 
       $handle = fopen($file_uplod_path, "r") or die("file cannot open"); 

       if ($handle) { 
        while (($line = fgets($handle)) !== false) 
        { 
         $lineArr = explode("\t", "$line");  

         $result = $this->model_name->insert_file_content($lineArr) ;  
        } 
        if (fclose($handle)) { 
         redirect(base_url()); 
        } 

       } 
       else{ 
        echo "file cannot open"; 
       } 
      } 
     }  
    } 
    else 
    { 
     echo "Moveing failed"; 

    } 

} 

在型號

public function insert_file_details($final_file,$file_type,$new_size) 
{ 

    $data = array(
     'file' => $final_file, 
     'type' => $file_type, 
     'size' => $new_size, 
    ); 

    if (!$this->db->insert('table_name', $data)) { 
     return false; 
    } 
    else{ 
     return true; 
    } 

} 

# inserting file in to DB 
public function insert_file_content($lineArr) 
{ 

    $data = array(
     'emp_id'  => $lineArr[0], 
     'date_data' => $lineArr[1], 
     'abc'  => $lineArr[2], 
     'def'  => $lineArr[3], 
     'entry'  => $lineArr[4], 
     'ghi'  => $lineArr[5], 
    ); 

    $this->db->insert('table_name', $data); 
} 
+0

未經測試。所以錯誤可能在那裏仔細處理那些 –

+0

是的好吧。現在它顯示我一個白色的屏幕,當我上傳任何文件 – Rajan

+0

改變這個'如果(move_uploaded_file($ final_file,$文件夾))' –

相關問題