【问题标题】:how to upload a csv file and update the mysql db? [closed]如何上传 csv 文件并更新 mysql 数据库? [关闭]
【发布时间】:2012-11-16 19:06:54
【问题描述】:

我想使用 csv 文件来更新 mysql scv 表。如何编码?我没有从事这项工作的经验。

<p>please select a scv file to upload</p>
<form action="index.php" method="post">
    <input type="file" name="scv"  />
    <input type="submit" value="submit" />
</form>
<?php 
    mysql_connect('localhost','root','admin');
    mysql_select_db('linjuming');
    // how to upload a scv file and insert or update the "csv" table?

?>

【问题讨论】:

标签: php mysql file-upload csv


【解决方案1】:

您的上传文件:

<form action="upload_target.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

你的upload_target.php

$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if ($ext == "csv" && $_FILES["file"]["error"] == 0)
{
    $target = "upload/" . $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $target);

    if (($handle = fopen($target, "r")) !== FALSE) 
    {
        while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
        {
            print_r($data);
        }

        fclose($handle);
    }
}

非常基本,几乎没有检查/验证。 print_r($data) 包含您现在可以插入数据库的一行 csv。

但是,我建议使用 PDO 或 MySQLi 来完成该任务,因为 PHP 的 mysql 函数将来会被弃用。

【讨论】:

    【解决方案2】:

    这有几个部分:

    首先,您的表单必须设置 enctype,如下所示:

    <form enctype="multipart/form-data"  action="index.php" method="post">
    

    否则将不接受文件上传。

    完成此操作后,您可以使用$_FILES 变量访问该文件。文件上传后,您可以像这样访问它:

    if (isset($_FILES["scv"])) {
        $file = $_FILES["scv"];
        $file_name = $file["name"];
        $ext = pathinfo($file_name, PATHINFO_EXTENSION);
        if ($ext!="CSV" && $ext!="TXT") {
            die('The file must be csv or txt format.');
        }
        $saveto_path_and_name = '/path/to/file.csv'; // Where you want to save the file
        move_uploaded_file($file["tmp_name"], $saveto_path_and_name);
    }
    

    保存文件后,您可以打开并导入它。这并非易事,但这里有一些入门代码:

    // Open the file for reading
    $handle = @fopen($saveto_path_and_name, "r") or die(__("Unable to open uploaded file!", "inventory"));
    // Grab the first row to do some checks
    $row = fgets($inv_file, 4096);
    // See if it's comma or tab delimited
    if (stripos($inv_row, "\t")) {
        $sep = "\t";
    } else {
        $sep = ",";
    }
    
    while ( ! feof($handle)) {
        $rowcount = 0;
        // Get the individual fields
        $inv_fields = explode($sep, $inv_row);
        $fields = array();
        // Iterate through the fields to do any sanitization, etc.
        foreach ($inv_fields as $field) {
            // Highly recommended to sanitize the variable $field here....
            $fields[] = $field;
            $rowcount++;
    }
        // This is where you would write your query statement to insert the data
        // This is just EXAMPLE code.  Use the DB access of your choice (PDO, MySQLi)
        $sql = vsprintf('INSERT INTO `table` (`column`, `column2`, ...) VALUES (%s, %d, ...)', $fields);
        // Get the next row of data from the file
        $row = fgets($inv_file, 4096);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2016-12-11
      相关资源
      最近更新 更多