duanli8577 2015-12-15 07:09
浏览 58
已采纳

如何上传文本文件并使用Codeigniter在sql中导入其数据?

I have a core PHP script which allows me to upload text file and import its data into sql database.

I want to do the same thing using Codeigniter. I tried by the screen shows me blank white screen when i put the code in CI application.

Below Is My core PHP script for uploading a file and Importing It:

<?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
    }

}

?>

I dont know how to do this in Codeigniter. Please guide me here

the Ci Form:

     <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>
  • 写回答

2条回答 默认 最新

  • doujia2021 2015-12-15 10:38
    关注

    In Controller

    ## 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";
    
        }
    
    }
    

    In Model

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆