2016-10-22 3 views
1

zip 파일에있는 6 개의 파일을 가져 오려고합니다. 먼저이 파일들에서 모든 데이터를 얻고 싶다면 그 파일들을 추출합니다. 하지만 현재는 첫 번째 파일 데이터 만 가져오고 있습니다. 스크립트가 두 번째 파일을 읽지 않았습니다. 나는이 문제를 없애는 방법을 이해하지 못한다. 읽는 방법 PHP에서 여러 CSV 파일 데이터

다음은 내 코드입니다.

<?php 

if ($_FILES) { 
    $filename  = $_FILES["zip_file"]["name"]; 
    $source   = $_FILES["zip_file"]["tmp_name"]; 
    $type   = $_FILES["zip_file"]["type"]; 
    $name   = explode(".", $filename); 
    $accepted_types = array(
     'application/zip', 
     'application/x-zip-compressed', 
     'multipart/x-zip', 
     'application/x-compressed' 
    ); 
    foreach ($accepted_types as $mime_type) { 
     if ($mime_type == $type) { 
      $okay = true; 
      break; 
     } 
    } 

    $continue = strtolower($name[1]) == 'zip' ? true : false; 
    if (!$continue) { 
     $message = "The file you are trying to upload is not a .zip file. Please try again."; 
    } 
    $target_path = "zip/" . $filename; 

    if (move_uploaded_file($source, $target_path)) { 
     $zip = new ZipArchive(); 
     $x = $zip->open($target_path); 
     $col = array(); 
     if ($x === true) { 
      for ($x = 0; $x < $zip->numFiles; $x++) { 

       $csv = $zip->getNameIndex($x); 

       $zip->extractTo("zip/"); 

       $csv_path = "zip/" . $csv; 

       if (($handle = fopen($csv_path, "r")) !== FALSE) { 
        fgetcsv($handle); 
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 

         $num = count($data); 

         for ($c = 0; $c < $num; $c++) { 
          $col[$c] = $data[$c]; 

         } 
         echo "<pre>"; 
         print_r($col); 
        } 

        fclose($handle); 

       } 

      } 


      $zip->close(); 
      unlink($target_path); 
      exit; 
     } 
     $message = "Your .zip file was uploaded and unpacked."; 
    } else { 
     $message = "There was a problem with the upload. Please try again."; 
    } 
} 
?> 

도움을 주시면 감사하겠습니다. 코드의이 부분에서

+0

내 대답은 당신이 그것으로 올바른 표시 할 수 있습니다, 도움이 있다면? 아니면 어떻게 작동하지 않았는지 말해 줄 수 있습니까? – HoldOffHunger

답변

0

봐 ...

<?php 
    // ...code... 
    $zip->extractTo("zip/"); 

    $csv_path = "zip/" . $csv; 

    if (($handle = fopen($csv_path, "r")) !== FALSE) { 
     fgetcsv($handle); 
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     // ...code... 
?> 

extractTo()는 파일의 압축을 풉니 다. 여섯 개의 파일이 있습니다. 그렇다면 fopen()을 수행하면 한 번 수행합니다. 각 파일에 대해 fopen()을 수행하려고합니다. 당신이 원하는거야 것은

...

<?php 
    // ...code... (files are extracted at this point) 
     $files = files('zip/'); 
     for($i = 0; i < count($files); $i++) { 
      $file = $files[$i]; 
      // ...do csv stuff here for $file 
     } 
    // ...code... 
?>