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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试