PHP上传CSV不会回显任何数据

时间:2013-08-03 10:48:00

标签: php csv

我有这个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文件正常复制到服务器

1 个答案:

答案 0 :(得分: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吗?