CSV上传没有浏览按钮

时间:2016-09-27 10:00:16

标签: php csv

我想在一个PHP页面运行时上传CSV的内容。我不希望任何浏览按钮上传CSV。每当页面运行时,页面都应找到已在PHP页面中定义路径的CSV,并且应将内容插入到表中。现在我收到与fopen相关的错误。

这是我的代码

<?php

//database connection details
$connect = mysql_connect('localhost', 'root', '');

if (!$connect) {
    die('Could not connect to MySQL: ' . mysql_error());
}

//your database name
$cid = mysql_select_db('test', $connect);

// path where your CSV file is located


define('CSV_PATH', 'D:/xamp/htdocs/test/');

// Name of your CSV file
$csv_file = CSV_PATH . "test.csv";

echo $csv_file;
if (($handle = fopen($csv_file, "r")) !== FALSE) {
    fgetcsv($handle);
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        for ($c = 0; $c < $num; $c++) {
            $col[$c] = $data[$c];
        }

        $col1 = $col[0];
        $col2 = $col[1];
        $col3 = $col[2];
        $col4 = $col[3];
        $col5 = $col[4];
        $col6 = $col[5];


        // SQL Query to insert data into DataBase
        $query = "INSERT INTO testcsv(Line,Part No,Make,Model,Year,Part Type) VALUES('" . $col1 . "','" . $col2 . "','" . $col3 . "','" . $col4 . "','" . $col5 . "','" . $col6 . "')";
        $s = mysql_query($query, $connect);
    }
    fclose($handle);
}

echo "File data successfully imported to database!!";
mysql_close($connect);
?>

我收到此错误

  

警告:fopen(D:/xamp/htdocs/test/test.csv):无法打开流:第22行的D:\ xamp \ htdocs \ test \ test.php中没有此类文件或目录   文件数据成功导入数据库!!

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我不确定你为什么会遇到这个特定的错误 - 可能会认为该文件不存在或者该目录不可读但您使用的是现已弃用的mysql_函数并直接嵌入变量sql - 因此很容易受到sql注入。但是,因为这看起来只是一个可能不是问题的测试。

此类事情的首选方法是将mysqliPDOprepared statements结合使用 - 以下是您如何实施该示例的示例 - 我测试了这个有不同的数据和数据库细节,它似乎工作正常。

define('CSV_PATH','D:/xamp/htdocs/test/');
$filepath = CSV_PATH . "test.csv"; 

/* database connection details */
$host   =   'localhost';
$uname  =   'xxx'; 
$pwd    =   'xxx'; 
$db     =   'xxx';

/* create db connection */
$con    =   new mysqli( $host, $uname, $pwd, $db );

/* construct required sql statement */
$sql='insert into `testcsv` (`Line`,`Part No`,`Make`,`Model`,`Year`,`Part Type`) values (?,?,?,?,?,?)';

/* create prepared statement */
$stmt=$con->prepare( $sql );


if( !$stmt ){

    echo 'error preparing sql statement!';
    $con->close();

} else {

    /* bind the columns to variables which will be populated later */
    /* use "i" for integer and "s" for string values */
    $stmt->bind_param( 'ssssss', $line,$part,$make,$model,$year,$type );

    /* access csv file */
    $file=new SplFileObject( $filepath );

    /* Process each row of the csv file */
    while( !$file->eof() ) {

        /* read the line into a variable */
        $data=$file->fgetcsv();

        if( !empty( $data ) ){
            /* assign a variable to each field value for this row */
            list( $line,$part,$make,$model,$year,$type )=$data;

            /* execute statement with the now defined variables */
            $stmt->execute();
        }
    }

    /* tidy up */
    $stmt->close();
    $con->close();

    echo 'database updated with new records from csv';
}