注意:未定义的变量:第 160 行的 csvFile,警告:fclose() 期望参数 1 为资源,第 160 行给出的 null
Notice: Undefined variable: csvFile inon line 160 ,Warning: fclose() expects parameter 1 to be resource, null given on line 160
I'm trying to upload a csv file into a php web page that will
allow me to insert/update the data into an sql database. But after I selected my csv file and clicked import it shows the error above, still a noob at PHP
advanced thanks for any help and suggestions!
<?php
if(isset($_POST['importSubmit'])){
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);
while(($line = fgetcsv($csvFile)) !== FALSE){
require_once('../../mysqlConnector/mysql_connect.php');
$query="UPDATE communitysurvey SET stationID = '".$line[0]."',userID= '{$_SESSION['username']}', question1 = '".$line[1]."', question2 = '".$line[2]."', question3 = '".$line[3]."', question4 = '".$line[4]."', question5 = '".$line[5]."', question6 = '".$line[6]."', question7 = '".$line[7]."', question8 = '".$line[8]."', question9 = '".$line[9]."',question10 = '".$line[10]."', question11 = '".$line[11]."', question12 = '".$line[12]."', question13 = '".$line[13]."', question14 = '".$line[14]."',question15 = '".$line[15]."', question16 = '".$line[16]."', question17 = '".$line[17]."', question18 = '".$line[18]."', question19 = '".$line[19]."',question20 = '".$line[20]."', question21 = '".$line[21]."',question22 = '".$line[22]."', comments1 = '".$line[23]."', comments2 = '".$line[24]."'";
$result=mysqli_query($dbc,$query);
}
}
}
fclose($csvFile);
}
?>
在存在$csvFile
变量的if
条件下移动fclose($csvFile);
:
<?php
if(isset($_POST['importSubmit'])){
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);
while(($line = fgetcsv($csvFile)) !== FALSE){
require_once('../../mysqlConnector/mysql_connect.php');
$query="UPDATE communitysurvey SET stationID = '".$line[0]."',userID= '{$_SESSION['username']}', question1 = '".$line[1]."', question2 = '".$line[2]."', question3 = '".$line[3]."', question4 = '".$line[4]."', question5 = '".$line[5]."', question6 = '".$line[6]."', question7 = '".$line[7]."', question8 = '".$line[8]."', question9 = '".$line[9]."',question10 = '".$line[10]."', question11 = '".$line[11]."', question12 = '".$line[12]."', question13 = '".$line[13]."', question14 = '".$line[14]."',question15 = '".$line[15]."', question16 = '".$line[16]."', question17 = '".$line[17]."', question18 = '".$line[18]."', question19 = '".$line[19]."',question20 = '".$line[20]."', question21 = '".$line[21]."',question22 = '".$line[22]."', comments1 = '".$line[23]."', comments2 = '".$line[24]."'";
$result=mysqli_query($dbc,$query);
}
fclose($csvFile);
}
}
}
?>
I'm trying to upload a csv file into a php web page that will allow me to insert/update the data into an sql database. But after I selected my csv file and clicked import it shows the error above, still a noob at PHP
advanced thanks for any help and suggestions!
<?php
if(isset($_POST['importSubmit'])){
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);
while(($line = fgetcsv($csvFile)) !== FALSE){
require_once('../../mysqlConnector/mysql_connect.php');
$query="UPDATE communitysurvey SET stationID = '".$line[0]."',userID= '{$_SESSION['username']}', question1 = '".$line[1]."', question2 = '".$line[2]."', question3 = '".$line[3]."', question4 = '".$line[4]."', question5 = '".$line[5]."', question6 = '".$line[6]."', question7 = '".$line[7]."', question8 = '".$line[8]."', question9 = '".$line[9]."',question10 = '".$line[10]."', question11 = '".$line[11]."', question12 = '".$line[12]."', question13 = '".$line[13]."', question14 = '".$line[14]."',question15 = '".$line[15]."', question16 = '".$line[16]."', question17 = '".$line[17]."', question18 = '".$line[18]."', question19 = '".$line[19]."',question20 = '".$line[20]."', question21 = '".$line[21]."',question22 = '".$line[22]."', comments1 = '".$line[23]."', comments2 = '".$line[24]."'";
$result=mysqli_query($dbc,$query);
}
}
}
fclose($csvFile);
}
?>
在存在$csvFile
变量的if
条件下移动fclose($csvFile);
:
<?php
if(isset($_POST['importSubmit'])){
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
if(is_uploaded_file($_FILES['file']['tmp_name'])){
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);
while(($line = fgetcsv($csvFile)) !== FALSE){
require_once('../../mysqlConnector/mysql_connect.php');
$query="UPDATE communitysurvey SET stationID = '".$line[0]."',userID= '{$_SESSION['username']}', question1 = '".$line[1]."', question2 = '".$line[2]."', question3 = '".$line[3]."', question4 = '".$line[4]."', question5 = '".$line[5]."', question6 = '".$line[6]."', question7 = '".$line[7]."', question8 = '".$line[8]."', question9 = '".$line[9]."',question10 = '".$line[10]."', question11 = '".$line[11]."', question12 = '".$line[12]."', question13 = '".$line[13]."', question14 = '".$line[14]."',question15 = '".$line[15]."', question16 = '".$line[16]."', question17 = '".$line[17]."', question18 = '".$line[18]."', question19 = '".$line[19]."',question20 = '".$line[20]."', question21 = '".$line[21]."',question22 = '".$line[22]."', comments1 = '".$line[23]."', comments2 = '".$line[24]."'";
$result=mysqli_query($dbc,$query);
}
fclose($csvFile);
}
}
}
?>