我正在尝试使用php将csv文件导入数据库表。问题是上传文件在接收页面中未被识别为csv文件。这是我的代码:
我的表格:
<form enctype='multipart/form-data' method='post' action="new_campaign.php" class="add_campaign_form">
<table class="add_campaign_table">
<tr>
<td><label>Campaign Name<label></td>
</tr>
<tr>
<td><input type="text" id="name" class="name" name="camp_name" value='' required/></td>
</tr>
<tr>
<td><label>Notes<label></td>
</tr>
<tr>
<td><textarea id="notes" name="camp_note" rows="4" cols="50" maxlength="250" placeholder="Campaign details" value='' required>
</textarea></td>
</tr>
<tr>
<td><label>Upload CSV File<label></td>
</tr>
<tr>
<td><input type="file" name="csv_file" id="csv" /></td>
</tr>
</table>
<input type="submit" class="submit" alt="Submit" width="120" height="30"/>
<br><br>
</form>
new_campaign.php
if(isset($_FILES) && $_FILES["file"]['error']==0){
if (($_FILES["file"]["type"] == "application/vnd.ms-excel")) {
if ($_FILES["file"]["error"] > 0) {
echo "error uploading the file";
}
else {
echo "hooray!";
}
}
else {
echo "this is not a csv file";
}
}
else{
echo "no files";
}
它一直在扔我:&#34;这不是一个csv文件&#34; 我在接收页面中获取其他字段值。有什么帮助吗?
答案 0 :(得分:0)
谢谢肖恩。这是工作代码:
if(isset($_FILES) && $_FILES["csv_file"]['error']==0){
//echo "file type: ".$_FILES["csv_file"]["type"];
if (($_FILES["csv_file"]["type"] == "text/csv")) {
if ($_FILES["text/csv"]["error"] > 0) {
echo "error uploading the file";
}
else {
echo "hooray!";
}
}
else {
echo "this is not a csv file";
}
}
else{
echo "no files";
}
答案 1 :(得分:-1)
您的输入为name="csv_file"
<input type="file" name="csv_file" id="csv" />
所以它应该是
if(isset($_FILES) && $_FILES["csv_file"]['error']==0){
if (($_FILES["csv_file"]["type"] == "application/vnd.ms-excel")) {
if ($_FILES["csv_file"]["error"] > 0) {
...
不是$_FILES["file"]['error']
/ $_FILES["file"]["type"]