alpha_full/admin/publier/scriptsExports/loadFunction.php
2026-04-06 22:58:51 +02:00

96 lines
2.8 KiB
PHP
Executable File

<?php
$nomFicAppelant = basename(__FILE__);
function loadDatas($fileName) {
// -----------------------------------------------------
// Init
// -----------------------------------------------------
global $mesg;
global $bddProd;
global $idPdoProd;
global $export_tag;
$startTime = microtime(true); // Démarrage du timer
// -----------------------------------------------------
// Function lire fic et --> mysql
// -----------------------------------------------------
$handle = fopen($fileName, "r");
if (!$handle) {
$mesg .= "<p>Impossible d'ouvrir le fichier : " . $fileName . "\n";
exit;
} else {
$indexBuffer = fread($handle, filesize($fileName));
}
fclose($handle);
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($fileName);
// Loop through each line
$z = 0;
$x = 0;
// ne fonctionne pas mais pas nécessaire, pas de commit ni rollback
$idPdoProd->beginTransaction();
try {
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
// Add this line to the current segment
$templine .= $line;
$x++;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
if (isset($debug[1]) && $debug[1] == '1' ) { $mesg .= '<br>templine = ' . $templine; }
// Execute query using PDO
$stmt = $idPdoProd->prepare($templine);
if (!$stmt->execute()) {
$mesg .= 'Erreur avec la requete';
if (isset($debug[1]) && $debug[1] == '1' ) {
$mesg .= '<br>templine = ' . $templine . ' : ' . $stmt->errorInfo()[2];
}
}
// Reset temp variable to empty
$templine = '';
if (isset($debug[1]) && $debug[1] == '1' ) {$mesg .= '<br>Lignes traitées : ' . $z; }
$z++;
}
}
// Commit the transaction ne fonction pas car le egin fonctionne pas
// $idPdoProd->commit();
} catch (PDOException $e) {
// In case of error, rollback the transaction
if ($idPdoProd->inTransaction()) {
$idPdoProd->rollBack();
}
$mesg .= 'Erreur PDO: ' . $e->getMessage();
if (isset($debug[1]) && $debug[1] == '1' ) {
$mesg .= '<br>' . $e->getTraceAsString();
}
}
$endTime = microtime(true); // Fin du timer
$elapsedTime = round($endTime - $startTime, 2); // Temps écoulé en secondes
if (isset($debug[1]) && $debug[1] == '1' ) {
$mesg .= 'Import ' . $export_tag . ' réalisé avec succès.<br>';
$mesg .= '<br> --> Fichier ' . $fileName . ' importé dans ' . $bddProd . ' . ' . $z . ' lignes traitées. <br>Temps écoulé pour import : ' . $elapsedTime . ' secondes. ';
}
return $mesg;
}
?>