gpt4 book ai didi

php - 如何在php中获取上传文件的完整路径?

转载 作者:可可西里 更新时间:2023-11-01 01:00:29 25 4
gpt4 key购买 nike

到目前为止我的代码片段:

if(isset($_POST['submit']))  {
$uploaddir = '/www/csvExtraction/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
}

还是报错

Undefined index: file in C:\wamp\www\csvExtraction\index.php


完整代码:

if(!$db)

die("no db");

if(!mysqli_select_db($db,"phptester"))

die("No database selected.");

if(isset($_POST['submit']))
{
$uploaddir = '/www/csvExtraction/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{

$handle = fopen("$uploadfile", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$import="INSERT into sample(id,name,address) values('$data[0]','$data[1]','$data[2]')";
mysqli_query($import) or die(mysql_error());
}
fclose($handle);
print "Import done";
}
}
else

{

print "<form action='index.php' method='post'>";

print "Choose file to import:<br><br>";

print "<input type='file' name='file' id='file'><br><br>";

//print "<input type='text' name='filename' size='20'><br>";

print "<input type='submit' name='submit' value='extract'></form>";

}
?>

最佳答案

我有一个解决方案。

<?php
$db = mysqli_connect("localhost", "root", "") or die("could not connect");

if(!$db)

die("no db");

if(!mysqli_select_db($db,"phptester"))

die("No database selected.");

if(isset($_POST['submit']))
{
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES["file"]["name"]);
echo $uploadfile;


if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile))
{

$handle = fopen("$uploadfile", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$import="INSERT into sample(id,name,address) values('$data[0]','$data[1]','$data[2]')";
mysqli_query($db,$import) or die(mysql_error());
}
fclose($handle);
print "Import done";
}
}
else
{
print "<form action='index.php' method='post' enctype='multipart/form-data'>";
print "Choose file to import:<br><br>";
print "<input type='file' name='file' id='file'><br><br>";
print "<input type='submit' name='submit' value='extract'></form>";
}
?>

关于php - 如何在php中获取上传文件的完整路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28603997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com