dragon071111 2015-09-14 08:48
浏览 63
已采纳

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

  • 写回答

3条回答

  • du6jws6975 2015-09-14 09:24
    关注

    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".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决