0

I'm using this script for importing a csv file to mysql database.

How can i display a progress bar for importing a csv file to db using jquery and php ?

I don't need te actual code, just some infos.

Thanks in advance.

    if ( $request->get( $_POST["action"] ) == "import" ) {

        $file = $upload->file_upload( "import", "media/import" );

        if ( file_exists( DIR_UPLOAD_PHOTO . "/media/import/" . $file ) ) {

            $file   = DIR_UPLOAD_PHOTO . "/media/import/" . $file;

            try {
                $dbh = new PDO("mysql:host=".HOST."; dbname=".DATABASE, USER, PASSWORD);

            }
            catch(PDOException $e) {
                echo $e->getMessage();
            }

            $handle = fopen( $file, "r" );
            $delimiter = '|';

            $dbh->beginTransaction();


            $stmt = $dbh->prepare("INSERT INTO products SET title = :title, price = :price
                                               ON DUPLICATE KEY UPDATE 
                                                           title = :title, price = :price"
            );


            fgets($handle);

            $rows = count(file($file));

            while ($line = fgetcsv($handle, 1000, $delimiter)) {

                    $line = array_map('trim', $line);


                    $stmt->bindParam(':title',          $line[0], PDO::PARAM_STR);
                    $stmt->bindParam(':price',          $line[1], PDO::PARAM_STR);

                    $stmt->execute();


            }


            $dbh->commit();
            fclose($handle);
            $dbh = null;


        }
    }
speedy
  • 375
  • 2
  • 14

1 Answers1

0

At last in MariaDB you get some Status-Information this way (don't know if it works in MySQL as well):

  • Fork the INSERT INTO ... Statement in an other process
  • and run it.
  • Use SHOW PROCESSLIST in your main thread to get the Status of the INSERT Statement.
Benvorth
  • 7,416
  • 8
  • 49
  • 70