csv 上传 下载

来源:互联网 发布:java微服务架构与实践 编辑:程序博客网 时间:2024/05/22 04:33
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 15-11-3
 * Time: 下午5:37
 */
//上传类
class Controller_Uploads extends Controller_Admin
{

    public function  action_index()
    {

        $this->title = ('海外仓入库|出库单');
        $view = View::factory('uploads/upload');
        $this->response->body($view);

    }

    //执行表单上传
    public function  action_upload()
    {

        $this->title = '上传成功';
        $view = View::factory('uploads/upload');

        //存储在服务器的文件的临时副本的名称
        $filename = $_FILES['file']['tmp_name'];

        if (empty($filename)) {
            echo '请选择要导入的表单!';
            exit;

        }
       // 显示第一次找到,要查找的字符串,以及后面的字符串。
        $result = stristr($_FILES["file"]["name"], ".csv");

        if ($result != false && $result == ".csv") {
            echo "你调入的是正确的!";
        } else {
            echo "请上传正确的表单格式!";
            exit;
        }
        //文件打开只读
        $handle = fopen($filename, 'r');

        // 从文件指针中读入一行并解析 CSV 字段
         fgetcsv($handle);

        while ($arr = fgetcsv($handle)) {
            $warehouse_date = $arr[0];
            $returned_parcel_id = $arr[1];
            $sku = $arr[2];
            $sku_name = $arr[3];
            $warehouse_quantity = $arr[4];
            $single_price = $arr[5];
            $money = $arr[6];
            $remark = $arr[7];


            $sql = "INSERT INTO  `warehouseorders_hwc` (`warehouse_date`, `returned_parcel_id`, `sku`, `sku_name`,
              `warehouse_quantity`,  `single_price`,`money`, `remark`)
              VALUES
               ('$warehouse_date','$returned_parcel_id','$sku','$sku_name','$warehouse_quantity','$single_price',
               '$money','$remark')";


            DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();


        }

    }

    // 上传表单模板
     public  function  action_template(){
         $this->title='上传模板';
         $view=View::factory('uploads/upload');
         define('EOL', "\n");

         $content = '日期,仓库名称,SKU,品名,数量,采购单价,采购金额,备注' . EOL;

         header("Content-Encoding: utf8");
         header("Content-type:调整海外仓入库单模板/csv");
         header("Content-Disposition:attachment;filename=调整海外仓入库单模板.csv");
         header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
         header('Expires:0');
         header('Pragma:public');
         echo $content;
         exit;
         $this->response->body($view);
     }


    //执行表单下载
    public function  action_download()
    {
        $this->title = '下载';
        $view = View::factory('uploads/upload');

        $sql = "SELECT `delivery_date`, `delivery_man`, `sku`, `sku_name`, `quantity`, `remark`
            FROM `deliveryorders_hwc` limit 0,10";
        $result = DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();
        define('EOL', "\n");

            $content = '日期,仓库名称,SKU,品名,数量,采购单价,采购金额,备注' . EOL;
            foreach ($result as $v) {
                $content .= $v['delivery_date'] . ',';
                $content .= $v['delivery_man'] . ',';
                $content .= $v['sku'] . ',';
                $content .= $v['sku_name'] . ',';
                $content .= $v['quantity'] . ',';
                $content .= $v['remark'];
                $content .= EOL;
            }
            header("Content-Encoding: utf8");
            header("Content-type:调整海外仓出口单/csv");
            header("Content-Disposition:attachment;filename=调整海外仓出库单.csv");
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
            header('Expires:0');
            header('Pragma:public');
            echo $content;
            exit;

    }
}
0 0
原创粉丝点击