0

我有这个 PHP 代码:

//start to process file
//first, move file on to server
echo("Uploading raw file...");
$file1_name=$_FILES['dd_submission_form']['tmp_name'];
move_uploaded_file ( "$file1_name","dd_submission_form.csv") or die("file did not copy<br>");
echo("Done<br/>");

//now open file using sequential access
echo("Opening file...");
$file_handle = fopen("dd_submission_form.csv", "r");

//read in just the first row at the moment to get call plan metadata (column headings)
$first_line=fgetcsv($file_handle);

while (($data = fgetcsv($file_handle, 1000, ',')) !== FALSE)
{
    echo $data[0];
}

fclose($file_handle);

print "Import done";

当我上传 CSV 文件时,它只是显示

上传原始文件...完成
打开文件...导入完成

即使我已经回$data[0];显,这应该显示 CSV 文件中 A 列的数据,但什么也没有

.csv 文件很好地复制到服务器

4

1 回答 1

0

我复制你的代码:

<form action="#" name='form1' enctype="multipart/form-data" method="POST">
    <input type="file" name="dd_submission_form">
    <input type="submit" name='invia'>
</form>

<?php
var_dump($_FILES);
var_dump($_POST);

if (isset($_FILES['dd_submission_form'])){

//start to process file
//first, move file on to server
    echo("Uploading raw file...");
    $file1_name = $_FILES['dd_submission_form']['tmp_name'];

    move_uploaded_file("$file1_name", "dd_submission_form.csv") or die("file did not copy<br>");

    echo("Done<br/>");

//now open file using sequential access
    echo("Opening file...");
    $file_handle = fopen("dd_submission_form.csv", "r");

//read in just the first row at the moment to get call plan metadata (column headings)
    $first_line = fgetcsv($file_handle);

    while (($data = fgetcsv($file_handle, 1000, ',')) !== FALSE) {
        echo $data[0];
    }

    fclose($file_handle);

    print "Import done";

} else {
    echo "<div>load file</div>";
}

我用下面的 CSV 测试它

col1,col2,col3,col4
1,2,3,4
a,b,c,d
I,II,III,IV
alpha,beta,gamma,delta

一切都按您的预期工作,所以我认为问题在于 CSV 的格式。

您可以发布您的 CSV 的一部分吗?

于 2013-08-03T11:32:25.183 回答