如何在html< input type ="文件"中获取文件路径>在PHP中? [英] How to get the file path in html <input type="file"> in PHP?
问题描述
< input type =file>
获取文件路径吗? 以下是我的代码:
index.php
< form action =csv_to_database.phpmethod =get>
< input type =filename =csv_file/>
< input type =submitname =uploadvalue =Upload/>
< / form>
和 csv_to_database.php
<?php
if(isset($ _ GET ['csv_file' ])){
$ row = 1; $($ $
$ b $ if(($ $ = $ GET =''csv_file'','r'))!== FALSE){
while(($ data = fgetcsv($ handle, 1000,,))!== FALSE){
$ num = count($ data);
在行$ row:< br />< / p> \\\
中回显< p> $ num字段;
$ row ++; ($ c = 0; $ c< $ num; $ c ++){
echo $ data [$ c]
。 < br /> \\\
;
}
}
fclose($ handle);
}
}
?>
我的问题是,只有当csv数据与我的php文件位于同一目录时才有效。我认为我需要获取文件路径,但是我不知道该怎么做。 解决方案
使用你现在得到的 $ _ GET
。您的文件基于 $ _ FILES [csv_file] [tmp_name]
。
本教程,基本上说你需要做这样的事情:
$ b $
<?php
if($ _FILES [csv_file] [error]> 0)
{
回显错误:。 $ _FILES [csv_file] [error]。 < br />;
}
else
{
echoUpload:。 $ _FILES [csv_file] [name]。 < br />;
回显类型:。 $ _FILES [csv_file] [type]。 < br />;
回声大小:。 ($ _FILES [csv_file] [size] / 1024)。 Kb< br />;
回声存储在:。 $ _FILES [ csv_file] [ tmp_name的值];
}
?>
你可以从那里开始。使用 move_uploaded_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>\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.
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 :)
这篇关于如何在html< input type ="文件"中获取文件路径>在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!