PHP上传文件时如何获取完整文件路径?
我真的很想知道当我用 PHP 上传文件时如何获得完整的文件路径?
这是我的问题...
我正在 PHP 中导入 csv 文件。上传文件不是问题,但用于导入 csv 文件的函数 fgetcsv() 需要 fopen。 fopen 是让我头疼的一个,因为它需要一个精确的文件路径,这意味着该文件应该与 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
:
<?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 这里只能获取传递的文件名通过变量$_FILES['csv_file']['name']
。我试图获取任何函数来获取互联网上 $_FILES 的完整文件路径,但找不到任何函数。
我对网络开发非常陌生,所以请耐心等待。请尽可能简单地回答...请帮助...
I really want to know how am I gonna get the full filepath when I upload a file in 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.
Here's my codes:
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
:
<?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.
I am very new to web development so pls be patient. Pls answer as simple as possible... Pls help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
['name']
指用户计算机上的原始文件名。这对你来说没有用,特别是因为它可能是空的。 (或者它可能包含假值,导致目录遍历漏洞。所以,避免它。)您需要使用
['tmp_name']
这是一个服务器绝对路径,如/ tmp/upload85728
可用于fopen()
或move_uploaded_file()
。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.)You need to use the
['tmp_name']
which is a server-absolute path like/tmp/upload85728
that can be used forfopen()
ormove_uploaded_file()
.我能够成功导入 csv 文件并将其存储在 mysql 数据库中。
以下是代码(实际上它与我的问题几乎相同,只是做了一些细微的更改,但效果很好):
index.php
:csv_import.php
:
I was able to successfully imported csv file and stored it in the mysql database.
Here are the the codes (actually its almost the same as my question with some slight changes with great effect):
index.php
:csv_import.php
:您需要在配置文件中或您想要使用的任何位置定义一个路径,然后无论您定义什么,都可以在项目中使用该变量。
因此,在放置此代码之后,您的完整文件路径将是 -
您可以使用上面的代码作为示例。
谢谢。
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.
so after the put this code your full filepath would be-
you can use above code as example.
Thanks.