PHP上传文件时如何获取完整文件路径?

发布于 2024-11-04 00:11:29 字数 1128 浏览 5 评论 0原文

我真的很想知道当我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红ご颜醉 2024-11-11 00:11:29

['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 for fopen() or move_uploaded_file().

撩心不撩汉 2024-11-11 00:11:29

我能够成功导入 csv 文件并将其存储在 mysql 数据库中。

以下是代码(实际上它与我的问题几乎相同,只是做了一些细微的更改,但效果很好):

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

<?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);
  }
 }
?>

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:

<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:

<?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);
  }
 }
?>
青瓷清茶倾城歌 2024-11-11 00:11:29

您需要在配置文件中或您想要使用的任何位置定义一个路径,然后无论您定义什么,都可以在项目中使用该变量。

i.e: define('FILE_UPLOADED_PATH','folder1/folder2/so on');

因此,在放置此代码之后,您的完整文件路径将是 -

FILE_UPLOADED_PATH.$_FILES['csv_file']['name'];

您可以使用上面的代码作为示例。

谢谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文