php 如何在PHP中获取html <input type="file">中的文件路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5814155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:34:11  来源:igfitidea点击:

How to get the file path in html <input type="file"> in PHP?

phpfile-uploadfilepath

提问by Newbie Coder

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

有人可以告诉我如何<input type="file">在 PHP 中使用 html 获取文件路径吗?

Here are my codes:

这是我的代码:

index.php

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

并在 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>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
   }
   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.

我的问题是,它仅在 csv 数据与我的 php 文件位于同一目录中时才有效。我想我需要获取文件路径,但我不知道该怎么做。

回答by Nanne

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

你不应该只使用$_GET你现在拥有的。您的文件基于$_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_fileif you want to move the file from the temp location, also explained in the tutorial :)

你可以从那里去。move_uploaded_file如果您想从临时位置移动文件,请使用,教程中也有说明:)

回答by Kokos

I think you would gain a lot from taking a look at the following link: POST method uploads.

我认为您会从以下链接中获益良多:POST 方法上传

First of all, you should change your form method to post, and add enctype="multipart/form-data".

首先,您应该将表单方法更改为post,并添加enctype="multipart/form-data".

Then you can get the temporary file path from $_FILES['csv_file']['tmp_name'].

然后你可以从$_FILES['csv_file']['tmp_name'].

回答by David Fells

In your call to fopen, use $_GET['csv_file']['tmp_name']- this points to the file on the server immediately after upload.

在调用 fopen 时,使用$_GET['csv_file']['tmp_name']- 这会在上传后立即指向服务器上的文件。