在PHP中上传文件时如何获得完整的文件路径? [英] How to get full filepath when uploading files in PHP?
问题描述
我真的想知道当我在PHP中上传文件时,我将得到完整的文件路径吗?
I really want to know how am I gonna get the full filepath when I upload a file in PHP?
这是我的问题...
我正在PHP中导入一个csv文件。上传文件不是问题,但是用于导入csv文件的函数fgetcsv()需要fopen。 fopen是一个让我头疼,因为它需要一个确切的文件路径,这意味着该文件应该在与php文件在同一目录。
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.
以下是我的验证码:
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
b>:
<?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在互联网的完整的文件路径,但找不到任何。
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.
我对网络开发非常新,所以要耐心等待。 Pls的答案尽可能简单... Pls help ...
I am very new to web development so pls be patient. Pls answer as simple as possible... Pls help...
推荐答案
]
是指用户计算机上的原始文件名。这对你没有用,特别是因为它可能是空的。 (或者它可以包含伪值,导致目录遍历漏洞利用。)
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.)
你需要使用 ['tmp_name ']
这是一个服务器绝对路径,例如 / tmp / upload85728
,可用于 fopen code>或
move_uploaded_file()
。
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()
.
这篇关于在PHP中上传文件时如何获得完整的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!