PHP接受所有csv文件

I am a PHP beginner writing a program to upload .csv files to a webpage. When testing my program, I have been using a file called Partitions.csv. However, now I need to upload files that are named differently and when I try to do this, my code will not accept the file. This is my HTML code where I upload my file.

<form action="uploader.v1.2.php" method="post" enctype="multipart/form-data">
        <input type="file" name="fileToUpload" id="fileToUpload" accept=".csv"/>
        <input type="submit" value="Upload File" name="submit" />
</form>

This is my PHP code for accepting the file.

$sheetData = array();

        if (($handle = fopen($_FILES["fileToUpload"]["name"], "r")) !== FALSE) 
        {
            while (($dat = fgetcsv($handle, 1000, ",")) !== FALSE) 
            {
                $sheetData[] = $dat;
            }
            fclose($handle);
        }
        else
        {
            echo '<script language="javascript">';
            echo 'alert("Error Uploading File")';
            echo '</script>';

        }

Could anyone tell me why I cannot upload files that have different file names. Any help would be appreciated. Just to reiterate, I am a PHP beginner

duancheng8000
duancheng8000 这是我得到的错误“警告:fopen(test.csv)[function.fopen]:无法打开流:845行/home/machacou/public_html/uploader.v1.2.php中没有这样的文件或目录”
大约 5 年之前 回复
doudi8298
doudi8298 打开php代码中的错误报告,看看是否生成了任何错误。另外,检查浏览器的控制台,看看是否报告了任何错误或警告。
大约 5 年之前 回复
dongtun2572
dongtun2572 只是文件的名称。我将同一文件从“Partitions.csv”重命名为“test.csv”,我的代码不接受名为“test.csv”的文件
大约 5 年之前 回复
duanlu6114
duanlu6114 “现在我需要上传不同名称的文件”,它只是已更改的文件的名称,还是名称和扩展名?
大约 5 年之前 回复

3个回答

Replace your upload handler lines with these:

if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], YOUR_UPLOAD_DIRECTORY_PATH_AND_FINAL_FILENAME)):
if (($handle = fopen(YOUR_UPLOAD_DIRECTORY_PATH_AND_FINAL_FILENAME, "r")) !== FALSE) 
    {
        while (($dat = fgetcsv($handle, 1000, ",")) !== FALSE) 
        {
            $sheetData[] = $dat;
        }
        fclose($handle);
    }
    else
    {
        echo '<script language="javascript">';
        echo 'alert("Error Uploading File")';
        echo '</script>';

    }
endif;

Where "YOUR_UPLOAD_DIRECTORY_PATH_AND_FINAL_FILENAME" represents a directory path and filename on your server: e.g: "/wamp/www/uploads/test.csv".

dongping1012
dongping1012 这解决了我的问题! 多谢,伙计
大约 5 年之前 回复



根据您的代码,没有理由为什么名称不同的文件不起作用。 最有可能的是CSV格式略有不同。</ p>

事实上,解析CSV可能非常复杂,尤其是当您需要支持“所有csv文件”时。 因此,我建议使用像这样的专用软件包,而不是使用内置的PHP函数:</ p>

https://github.com/thephpleague/csv </ p>
</ div>

展开原文

原文

Based on your code, there's no reason why a file with a different name wouldn't work. Most likely it is the CSV format that is slightly different.

In fact, parsing CSV can be very complex especially if you need to support "all csv files". So rather than use the built-in PHP functions, which can be quite limited, I'd suggest using a specialized package such as this one:

https://github.com/thephpleague/csv

douqi3064
douqi3064 感谢您的建议! 我会尝试使用这个包
大约 5 年之前 回复



首先,属性 accept </ code>必须包含MIME类型:</ p>

< blockquote>

text /逗号分隔值,text / csv,text / anytext,application / csv,
application / excel,application / vnd.msexce,application / vnd.ms-excel </ p>

</ blockquote>

其次,CSV是文本格式,文件扩展名可能与.csv不同。
如果要按扩展名过滤,可以使用FileAPI在clien端进行 ( https://w3c.github.io/FileAPI/ )。</ p>
< / DIV>

展开原文

原文

First of all, the attribute accept must contains MIME type:

text/comma-separated-values, text/csv, text/anytext, application/csv, application/excel, application/vnd.msexce, application/vnd.ms-excel

Secondly, CSV is a text format and file extension may be differ than .csv. But if you want to filter by extension, you can do it on clien-side using FileAPI (https://w3c.github.io/FileAPI/).

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐