我正在尝试将字段上传到mysql数据库。用户可以手动输入该字段或上载包含该字段的.csv文件来上传该字段。手动输入效果很好但是当我尝试使用.csv上传时,它看不到文件并跳到我的代码的“else”段。这是我的代码:
<?php
$classname = $_POST['classname'];
$classname = stripslashes($classname);
$classname = mysql_real_escape_string($classname);
$save = $_POST['save'];
$save = stripslashes($save);
$save = mysql_real_escape_string($save);
$myfile = $_POST['myfile'];
$myfile = stripslashes($myfile);
$myfile = mysql_real_escape_string($myfile);
$import = $_POST['import'];
$import = stripslashes($import);
$import = mysql_real_escape_string($import);
if($classname && $save)
{
$choose_db;
$sql="INSERT INTO classes (`class`) VALUES ('$classname')";
$res=mysql_query($sql, $connect) or die (mysql_error());
if ($res)
{
echo "<script type='text/javascript'>
alert('Class added succefully');
window.location='../classes.php'
</script>";
}
else
{
echo "<script type=text/javascript'>
alert('Class not added');
window.location('../classes.php');
</script";
}
}
elseif ($import && $myfile)
{
echo $filename = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0)
{
$thefile=fopen($filename, "r");
while (($thedata = fgetcsv($thefile, 10000, ",")) !== FALSE)
{
$sql_file="INSERT INTO classes (`class`) VALUES ('$thedata[1]')";
$res_file=mysql_query($sql_file, $connect) or die (mysql_error());
if (!$res_file)
{
echo "<script type='text/javascript'>
alert ('Invalid file type!');
window.location = '../classes.php';
</script>
";
}
}
fclose($thefile);
echo "<script type='text/javascript'>
alert ('File Imported Successfully!');
window.location = '../classes.php';
</script>
";
}
}
else
{
echo "<script type='text/javascript'>
alert('Fill the form or choose a CSV file');
window.location='../classes.php'
</script>";
}
?>
这是html表单
<form class="form-horizontal" method="post" name="addclass" action="popups/saveclass.php">
<div style="margin:5px" class="form-group">
<label class="col-sm-12">New Class Name:</label>
<div class="col-sm-12">
<input type="text" name="classname" class="form-control" placeholder="Class Name" />
</div>
</div>
<center><input type="submit" name="save" value="Save" id="submit" style="width:200px; height:30px; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#002435; font-size:14px; cursor:pointer"/>
</center>
</form>
<form class="form-horizontal" method="post" name="uploadcsv" action="popups/saveclass.php" enctype="multipart/form-data">
<br /><center>Or Upload CSV file</center><br />
<div style="margin:5px" class="form-group">
<div class="col-sm-12">
<input type="file" name="myfile" class="form-control" />
</div>
</div>
<center><input type="submit" name="import" value="Import" style="width:200px; height:30px; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; color:#002435; font-size:14px; cursor:pointer"/></center>
</form>
答案 0 :(得分:2)
文件上传仅在$_FILES
,而非$_POST
。所以$myfile =
$ _ POST [&#39; myfile&#39;]; won't do anything, get rid of everything related to
$ myfile`。
由于您未将其存储在数据库中,因此无需在stripslashes
上致电mysql_real_escape_string
或$_POST['import']
。
您使用$_FILES['file']
,但该输入的名称为myfile
,因此应为$_FILES['myfile']
。
所以代码应该是:
elseif (isset($_POST['import'], $_FILES['myfile'])) {
echo $filename = $_FILES["myfile"]["tmp_name"];
if ($_FILES["myfile"]["size"] > 0)
{
...
}
}