dtt2012 2011-04-28 05:45
浏览 249
已采纳

如何在PHP中使用html <input type =“file”>获取文件路径?

Can somebody pls tell me how to get the filepath using html <input type="file"> in PHP?

Here are my codes:

index.php

<form action="csv_to_database.php" method="get" >
 <input type="file" name="csv_file" />
 <input type="submit" name="upload" value="Upload" />
</form>

and in csv_to_database.php

<?php

 if (isset($_GET['csv_file'])) {

 $row = 1;

  if (($handle = fopen($_GET['csv_file'], "r")) !== FALSE) {
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>
";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />
";
    }
   }
   fclose($handle);
  }

 } 
?>

My problem is, it only works when the csv data is in the same directory as my php files. I think I need to get the file path but I don't know how to do it.

  • 写回答

3条回答 默认 最新

  • dscpg80066 2011-04-28 05:53
    关注

    You shouldn't just use the $_GET you've got now. Your file is based in $_FILES["csv_file"]["tmp_name"].

    Best you review this tutorial, that basically says you need to do something like this:

    <?php
    if ($_FILES["csv_file"]["error"] > 0)
      {
      echo "Error: " . $_FILES["csv_file"]["error"] . "<br />";
      }
    else
      {
      echo "Upload: " . $_FILES["csv_file"]["name"] . "<br />";
      echo "Type: " . $_FILES["csv_file"]["type"] . "<br />";
      echo "Size: " . ($_FILES["csv_file"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["csv_file"]["tmp_name"];
      }
    ?>
    

    And you can go from there. Use move_uploaded_file if you want to move the file from the temp location, also explained in the tutorial :)

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?