2

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.

1
  • If my answer helped, can you mark it as correct? Or can you tell me how it didn't work? Commented Oct 27, 2016 at 22:15

1 Answer 1

1

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...
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.