dragon87836215 2015-07-08 14:57
浏览 52

PHP csv上传适用于mac但不适用于Windows

I have a csv upload plugin for wordpress. I can upload the files on a mac but on a windows pc it fails to upload. The csv files are created on the pc with utf-8 encoding.

if (isset($_POST) && !empty($_POST)) {

if (isset($_FILES) && $_FILES['csv_file']['size'] > 0 && $_FILES['csv_file']['type'] === 'text/csv') {

        global $wpdb;

    ini_set("auto_detect_line_endings", true);

    $start_row = (int) $_POST['start_row'];

    /*
     * Get CSV data and put it into an array
     */
    $fileData = file_get_contents($_FILES['csv_file']['tmp_name']);
    $lines = explode(PHP_EOL, $fileData);
    $csv = array();

    foreach ($lines as $line) {
            $csv[] = str_getcsv($line);
    }

    /*
     * Put each row into the database
     */
    $x = 1;
    $insert_count = 0;
    $insert_output = array();

    $wpdb->query('TRUNCATE TABLE table');

    foreach ($csv as $data) {

            if ($x >= $start_row) {

              $date = fix_date($data[0]);

              $sql = "
                INSERT INTO table ( date, column_1, column_2, column_3, column_4, column_5, column_6, column_7 ) 
                VALUES ( '" . $date . "', '" . addslashes( $data[1] ) . "', '" . utf8_encode( $data[2] ) . "', '" . addslashes( $data[3])  . "', '" . $data[4] . "', '" . addslashes( $data[5] ) . "', '" . $data[6] . "', '" . $data[7] . "' )
                ";

                $wpdb->query($sql)/* or die($sql)*/;
                $insert_output[] = $insert_count . '. Added: ' . $data[1] . ' - ' . $data[3] . '<br />';
                $insert_count++;

            }

            $x++;
        }

    echo '<div class="csv_success">Success. ' . number_format($insert_count) . ' rows uploaded.</div>';

} else {

    echo '<div class="csv_failure">Please make sure the file you uploaded is a CSV.</div>';

}

}

Any ideas how I can get this to work on windows and mac?

Cheers

  • 写回答

1条回答 默认 最新

  • douhan4812 2015-07-09 09:13
    关注
    <?php
    
    ini_set('max_execution_time', 0);
    $con = mysql_connect("localhost", "", "") or die("not connect");
    
    mysql_select_db("demo") or die("select db");
    
     function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
        return $line_of_text;
     }
    
    
    
      //Set path to CSV file
        $csvFile = 'page.csv';
    
     $csv = readCSV($csvFile);
     //echo count($csv);     
     for ($i=1; $i <count($csv) ; $i++) { 
    
    
    
        $sql="insert into demo1     (name,bdate,phonenumber ) values('".$csv[$i][0]."','".$csv[$i][1]."' ,'".$csv[$i][2]."')";
        mysql_query($sql);
    
    
    
    
     }
    echo 'done';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP