vikash
vikash

Reputation: 475

How to read Multiple CSV file data in php

I am trying to import 6 files which are in zip files. First I extract those files after that I want to get all the data in these files. But currently I am getting only the first file data. The script had not read the second file. I don't understand how to get rid from this problem.

Here is my code.

<?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.";
    }
}
?>

Any help would be Highly appreciated.

Upvotes: 2

Views: 2139

Answers (1)

HoldOffHunger
HoldOffHunger

Reputation: 20881

Look at this part of your code...

<?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() extracts the files. There are six files, you said. Then you do fopen(), and you do it once. You want to do that fopen() on each of the files.

What you'll want is...

<?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...
?>

Upvotes: 1

Related Questions