2016-10-22 18 views
1

zipファイルにある6つのファイルをインポートしようとしています。最初に、これらのファイルを抽出した後、これらのファイルのすべてのデータを取得します。しかし、現在私は最初のファイルデータだけを取得しています。スクリプトは2番目のファイルを読み取っていませんでした。私はこの問題を取り除く方法を理解していません。読み方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は()ファイルを抽出します。 6つのファイルがあります。それであなたは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... 
?> 
関連する問題