問題描述
誰(shuí)能告訴我如何在 PHP 中使用 html <input type="file">
獲取文件路徑?
Can somebody pls tell me how to get the filepath using html <input type="file">
in 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>
和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);
}
}
?>
我的問題是,它僅在 csv 數(shù)據(jù)與我的 php 文件位于同一目錄中時(shí)才有效.我想我需要獲取文件路徑,但我不知道該怎么做.
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.
推薦答案
你不應(yīng)該只使用你現(xiàn)在擁有的 $_GET
.您的文件基于 $_FILES["csv_file"]["tmp_name"]
.
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"];
}
?>
你可以從那里去.如果您想從臨時(shí)位置移動(dòng)文件,請(qǐng)使用 move_uploaded_file
,教程中也有說明:)
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 :)
這篇關(guān)于如何獲取html中的文件路徑<input type="file">在 PHP 中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!