0

csvファイルを使用してmysqlscvテーブルを更新したいと思います。コーディング方法は?私はこの仕事をした経験がありません。

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

?>

ここに画像の説明を入力してください

4

2 に答える 2

1

これにはいくつかの部分があります。

まず、フォームには次のように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);
}
于 2012-11-28T20:07:03.347 に答える
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の1行が含まれています。

ただし、PHPのmysql関数は将来非推奨になるため、そのタスクにはPDOまたはMySQLiを使用することをお勧めします。

于 2012-11-28T20:09:51.970 に答える