php 在PHP中上传文件时如何获取完整文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5816666/
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
How to get full filepath when uploading files in PHP?
提问by Newbie Coder
I really want to know how am I gonna get the full filepath when I upload a file in PHP?
我真的很想知道当我在 PHP 中上传文件时如何获得完整的文件路径?
Here's my my problem...
这是我的问题...
I am importing a csv file in PHP. Uploading a file isn't a problem but the function used to import csv files which is fgetcsv() requires fopen. fopen is the one giving me a headache because it requires an exact filepath which means that the file should be in the same directory with the php file. What if the user gets a file from a different directory.
我正在用 PHP 导入一个 csv 文件。上传文件不是问题,但用于导入 csv 文件的函数 fgetcsv() 需要 fopen。fopen 是让我头疼的一个,因为它需要一个确切的文件路径,这意味着该文件应该与 php 文件位于同一目录中。如果用户从不同的目录获取文件怎么办。
Here's my codes:
这是我的代码:
index.php
:
index.php
:
<form action="csv_to_database.php" method="POST" enctype="multipart/form-data">
<input type="file" name="csv_file" />
<input type="submit" name="upload" value="Upload" />
</form>
csv_import.php
:
csv_import.php
:
<?php
if ($_FILES['csv_file']['error'] > 0) {
echo "Error: " . $_FILES['csv_file']['error'] . "<br />";
}else{
if (($handle = fopen($_FILES['csv_file']['name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c=0; $c < count($data) ; $c++) {
echo $data[$c] . " ";
}
echo "<br />";
}
fclose($handle);
}
}
?>
fopen here can only get the filename which is passed by the variable $_FILES['csv_file']['name']
. I was trying to get any functions to get the full filepath for $_FILES in the internet but can't find any.
这里的 fopen 只能得到变量传递过来的文件名$_FILES['csv_file']['name']
。我试图获取任何函数来获取 Internet 中 $_FILES 的完整文件路径,但找不到任何函数。
I am very new to web development so pls be patient. Pls answer as simple as possible... Pls help...
我对网络开发很陌生,所以请耐心等待。请尽可能简单地回答...请帮助...
回答by mario
The ['name']
refers to the original filename on the users computer. That's no use to you, in particular because it might be empty. (Or it can contain fake values, causing a directory traversal exploit. So, just avoid it.)
该['name']
指用户计算机上的原始文件名。这对您没有用,特别是因为它可能是空的。(或者它可能包含假值,导致目录遍历漏洞。所以,避免它。)
You need to use the ['tmp_name']
which is a server-absolute path like /tmp/upload85728
that can be used for fopen()
or move_uploaded_file()
.
您需要使用['tmp_name']
which 是服务器绝对路径,例如/tmp/upload85728
可用于fopen()
or move_uploaded_file()
。
回答by Newbie Coder
I was able to successfully imported csv file and stored it in the mysql database.
我能够成功导入 csv 文件并将其存储在 mysql 数据库中。
Here are the the codes (actually its almost the same as my question with some slight changes with great effect):
这里是代码(实际上它几乎与我的问题相同,但有一些细微的变化,效果很好):
index.php
:
index.php
:
<form action="csv_import.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="csv_file" />
<input type="submit" name="upload" value="Upload" />
</form>
csv_import.php
:
csv_import.php
:
<?php
if ($_FILES['csv_file']['error'] > 0) {
echo "Error: " . $_FILES['csv_file']['error'] . "<br />";
}else{
if (($handle = fopen($_FILES['csv_file']['tmp_name'], "r")) !== FALSE) {
$dbconn = mysql_connect("localhost", "root", "") or die("Couldn't connect to server!");
mysql_select_db("csv_test") or die("Couldn't find database!");
$ctr = 1; // used to exclude the CSV header
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($ctr > 1) mysql_query("INSERT INTO ninja_exer (name, village, country) VALUES ('$data[1]', '$data[2]', '$data[3]')");
else $ctr++;
}
fclose($handle);
}
}
?>
回答by Chandresh M
you need to define a path into your config file or wherever you want to use and then that variable whatever you define, you can use in you project.
你需要在你的配置文件或任何你想使用的地方定义一个路径,然后无论你定义什么变量,你都可以在你的项目中使用。
i.e: define('FILE_UPLOADED_PATH','folder1/folder2/so on');
so after the put this code your full filepath would be-
所以在放置这段代码之后,你的完整文件路径将是-
FILE_UPLOADED_PATH.$_FILES['csv_file']['name'];
you can use above code as example.
您可以使用上面的代码作为示例。
Thanks.
谢谢。