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

时间:2015-07-08 14:57:32

标签: php wordpress csv

我有一个用于wordpress的csv上传插件。我可以在Mac上上传文件但在Windows PC上无法上传。使用utf-8编码在PC上创建csv文件。

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>';

}

}

我有什么想法可以让它在Windows和Mac上运行?

干杯

1 个答案:

答案 0 :(得分:0)

<?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';
?>