到目前为止我的代码段:
if(isset($_POST['submit'])) {
$uploaddir = '/www/csvExtraction/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
}
我仍然收到此错误
未定义的索引: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>";
}
?>
答案 0 :(得分:2)
我得到了一个解决方案。
<?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>";
}
?>
答案 1 :(得分:1)
请检查$_FILES['file']['name'] != ''
之前
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
然后检索它
另外,请检查print_r($_FILES)
并查看是否有任何内容?
答案 2 :(得分:1)
检查您的表单是enctype="multipart/form-data"
是否包含此attr
答案 3 :(得分:0)
请确保您的html代码中的内容正确无误。
<input type="file" name="file">