我有这个代码,但它只占用我的csv文件的第一行..我已经搜索到互联网,但没有一个让我理解,因为我是非常新的PHP ..对不起,如果这个问题是重复的。
if(isset($_POST["Import"])){
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$sql = "INSERT INTO book (book_id,total_book,price,title,author,surname,genre,location) values
('$getData[0]','$getData[1]','$getData[2]','$getData[3]','$getData[4]','$getData[5]','$getData[6]','$getData[7]')";
$result = mysqli_query($con, $sql);
var_dump($sql);die;
// var_dump(mysqli_error_list($con));
// exit();
if(!isset($result))
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"bookList.php\"
</script>";
}
else {
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"bookList.php\"
</script>";
}
}
fclose($file);
}
}
答案 0 :(得分:0)
您可以尝试我的方式来处理用户出勤保存在数据库中。 这里我的代码下面我将上传到数据保存在server / db。
//after form post with file
if (isset($_POST["frmUplaod"])) {
extract($_POST);
$filename = basename($_FILES['attendance_file']['name']);
$targetfolder = 'Attendance/'. $filename .'';
if ($filename != "") {
move_uploaded_file($_FILES['attendance_file']['tmp_name'], $targetfolder);
chmod($targetfolder, 0777);
$msg = "Attendance file succesfully updated";
}
}
之后我使用以下代码处理此csv文件:
function MakeID($id) {
$refine_id = "";
if (strlen($id) == "1") {
$refine_id = "SUL00" . $id;
} elseif (strlen($id) == "2") {
$refine_id = "SUL0" . $id;
} elseif (strlen($id) == "3") {
$refine_id = "SUL" . $id;
} else {
$refine_id = "SUL".$id;
}
return $refine_id;
}
//336668677
//Read directory
$FileArray = scandir("Attendance");
$i = 0;
$existsdate = array();
foreach ($FileArray as $f) {
$filename = "Attendance/" . $f;
$extensiondebug='';
if(!empty($f))
{
$splitex=explode(".",$f);
$extensiondebug=$splitex[1];
}
if ($extensiondebug == "csv") {
$row = 1;
if (($handle = fopen("Attendance/".$f, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$terminal_id=1;
$emp_code=MakeID($data['1']);
$pd=split('/',$data[0]);
@$raw_date=$pd[2]."-".$pd[1]."-".$pd[0];
$unixtime=strtotime($raw_date);
$date=date('Y-m-d',$unixtime);
//echo $date."<br>";
if($data['1']!="ID" && $date!="1970-01-01")
{
if($data['6']!="00:00" && $data['6']!="0:00")
{
//echo $data['6']."<br>";
//echo $date;
$time=date('g:i:s',strtotime($data['6']));
//exit();
$res = $con->existsByCondition("attendance_raw", " employee_id='$emp_code' AND date='$date' AND time='$time'");
if ($res <= 0) {
$array = array(
"machine_id" => $terminal_id,
"date" => $date,
"time" => $time,
"employee_id" => $emp_code,
"result" => "1"
);
$con->insert("attendance_raw", $array);
}
}
if($data['7']!="00:00" && $data['7']!="0:00")
{
$time2=date('g:i:s',strtotime($data['7']));
$res = $con->existsByCondition("attendance_raw", " employee_id='$emp_code' AND date='$date' AND time='$time2'");
if ($res <= 0) {
$array = array(
"machine_id" => $terminal_id,
"date" => $date,
"time" => $time2,
"employee_id" => $emp_code,
"result" => "1"
);
$con->insert("attendance_raw", $array);
}
}
}
//echo "<br />\n";
}
fclose($handle);
}
rename($filename, 'read/' . $filename);
echo "file moved to specified directory.";
}
}
我希望这可以帮助您完成csv文件过程。
答案 1 :(得分:0)
谢谢大家的帮助..我已经解决了问题,我的代码中的问题只是我使用的分隔符,我使用了错误的分隔符