2017-05-16 7 views
2

다음은 CSV 파일을 업로드하는 코드입니다. 일부 파일은 제대로 업로드되지만 올바르게 업로드되지는 않습니다. 그것은 "회원 데이터가 성공적으로 삽입되었습니다."라고 말합니다. 그러나 db를 볼 때, 레코드는 없습니다. 코드를 한 번 확인하고 오류가 있으면 알려주십시오.CSV 파일이 업로드되지 않습니다.

<?php 
//load the database configuration file 
include 'csv_css.css'; 
include 'dbConfig.php'; 

if(!empty($_GET['status'])){ 
    switch($_GET['status']){ 
     case 'succ': 
      $statusMsgClass = 'alert-success'; 
      $statusMsg = 'Members data has been inserted successfully.'; 
      break; 
     case 'err': 
      $statusMsgClass = 'alert-danger'; 
      $statusMsg = 'Some problem occurred, please try again.'; 
      break; 
     case 'invalid_file': 
      $statusMsgClass = 'alert-danger'; 
      $statusMsg = 'Please upload a valid CSV file.'; 
      break; 
     default: 
      $statusMsgClass = ''; 
      $statusMsg = ''; 
     } 
} 
?> 
<div class="container"> 
<?php if(!empty($statusMsg)){ 
    echo '<div class="alert '.$statusMsgClass.'">'.$statusMsg.'</div>'; 
    } 
?> 
    <div class="panel panel-default"> 
    <div class="panel-heading"> 
     Members list 
     <a href="javascript:void(0);" onclick="$('#importFrm').slideToggle();">Import Members</a> 
    </div> 
    <div class="panel-body"> 
     <form action="import_usait.php" method="post" enctype="multipart/form-data" id="importFrm"> 
      <input type="file" name="file" /> 
      <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT"> 
     </form> 
     <table class="table table-bordered"> 
      <thead> 
       <tr> 
        <th>ContactOwner</th> 
        <th>LeadSource</th> 
        <th>Firstname</th> 
        <th>Lastname</th> 
        <th>Accountname</th> 
        <th>Title</th> 
        <th>EmailID</th> 
        <th>Department</th> 
        <th>Industry</th> 
        <th>Phone</th> 
        <th>Mobile</th> 
        <th>Fax</th> 
        <th>DOB</th> 
        <th>Asssistant</th> 
        <th>Asstphone</th> 
        <th>ReportsTo</th> 
        <th>LinkedIn</th> 
        <th>Street</th> 
        <th>OtherStreet</th> 
        <th>City</th> 
        <th>State</th> 
        <th>Zip</th> 
        <th>Country</th> 
        <th>Description</th> 
       </tr> 
      </thead> 
      <tbody> 
<?php 
    //get records from database 
    $query = $db->query("SELECT * FROM contact ORDER BY id DESC"); 
    if($query->num_rows > 0){ 
     while($row = $query->fetch_assoc()){ ?> 
       <tr> 
        <td><?php echo $row['id']; ?></td> 
        <td><?php echo $row['ContactOwner']; ?></td> 
        <td><?php echo $row['LeadSource']; ?></td> 
        <td><?php echo $row['Firstname']; ?></td> 
        <td><?php echo $row['Lastname']; ?></td> 
        <td><?php echo $row['Accountname']; ?></td> 
        <td><?php echo $row['Title']; ?></td> 
        <td><?php echo $row['EmailID'];?></td> 
        <td><?php echo $row['Department'];?></td> 
        <td><?php echo $row['Industry'];?></td> 
        <td><?php echo $row['Phone']; ?></td> 
        <td><?php echo $row['Mobile']; ?></td> 
        <td><?php echo $row['Fax'];?></td> 
        <td><?php echo $row['DOB'];?></td> 
        <td><?php echo $row['Asssistant'];?></td> 
        <td><?php echo $row['Asstphone'];?></td> 
        <td><?php echo $row['ReportsTo'];?></td> 
        <td><?php echo $row['LinkedIn'];?></td> 
        <td><?php echo $row['Street'];?></td> 
        <td><?php echo $row['OtherStreet'];?></td> 
        <td><?php echo $row['City']; ?></td> 
        <td><?php echo $row['State'];?></td> 
        <td><?php echo $row['Zip'];?></td> 
        <td><?php echo $row['Country'];?></td> 
        <td><?php echo $row['Description'];?></td> 
       </tr> 
<?php 
     } 
    }else{ 
?> 
       <tr><td colspan="25">No member(s) found.....</td></tr> 
<?php } ?> 
      </tbody> 
     </table> 
    </div> 
    </div> 
</div> 

import_usait.php 파일 :

<?php 
//load the database configuration file 
include 'dbConfig.php'; 

if(isset($_POST['importSubmit'])){ 

    //validate whether uploaded file is a csv file 
    $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'])){ 

      //open uploaded csv file with read only mode 
      $csvFile = fopen($_FILES['file']['tmp_name'], 'r'); 

      //skip first line 
      fgetcsv($csvFile); 

      //parse data from csv file line by line 
      while(($line = fgetcsv($csvFile)) !== FALSE){ 
       //check whether member already exists in database with same Category 
       //insert member data into database 
       $db->query("INSERT INTO contact 
           (ContactOwner, LeadSource, Firstname, 
           Lastname,Accountname, Title, 
           EmailID, Department,Industry, 
           Phone, Mobile, Fax, DOB, Assistant, 
           Asstphone, ReportsTo, LinkedIn, Street, 
           OtherStreet, City, State, Zip, 
           Country, Description) 
         VALUES 
          ('".$line[0]."','".$line[1]."','".$line[2]. 
          "','".$line[3]."','".$line[4]."','".$line[5]. 
          "','".$line[6]."','".$line[7]."','".$line[8]. 
          ",'".$line[9]."','".$line[10].",'".$line[11]."','".$line[12].",'".$line[13]. 
          "','".$line[14].",'".$line[15]."','".$line[16].",'".$line[17]. 
          "','".$line[18].",'".$line[19]."','".$line[20].",'".$line[21]. 
          "','".$line[22].",'".$line[23]."')"); 
      } 

      //close opened csv file 
      fclose($csvFile); 

      $qstring = '?status=succ'; 
     }else{ 
      $qstring = '?status=err'; 
     } 
    }else{ 
     $qstring = '?status=invalid_file'; 
    } 
} 

//redirect to the listing page 
header("Location: usa_it.php".$qstring); 
+3

SQL 주입을 방지하기 위해 prepared statement에 대해 알아보기 – Jens

+2

크기에 문제가 있습니까? – Jens

+1

로그를 확인하십시오. 어딘가에서 잘못된 것이 있으면 그곳에 나타나야합니다. – junkfoodjunkie

답변

1

나는이에게 자신을 실행하지 않은,하지만 난 당신의 마임 검사가 당신이 그것을 확인하고있는 시점에서 미스이다 생각하고 있어요. 먼저 is_uploaded_file()이 있는지 확인한 다음 mime을 확인하십시오. 이 단계에서 MIME 형식은 브라우저에서보고되며 이상하거나 잘못되었거나 누락되거나 위험 할 수 있습니다. 믿지 마라.

관련 문제