jeudi 29 décembre 2016

Shuffle and miss matching files

I have been researching for this issue for 2 days now, tested whatever possible solution may work, but nothing has been so far, I am desperate for some help to resolve the issue.

I have this code,

<!-- language: lang-php -->
<?php 
include 'db.php'; 
$folder = 'C:/folder/';
$files = scandir($folder);
$files = array_diff(scandir($folder), array('.', '..')); 
shuffle($files);
$i = 0;
foreach($files as $file){
    $file_name = $con->real_escape_string($file);
    $hash = md5($con->real_escape_string($file));
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    $insert_row = $con->query("INSERT IGNORE INTO files (file_name, hash, ext) 
    VALUES('$file_name', '$hash', '$ext')");
    if($insert_row){
        if($con->insert_id == 0){
            echo 'Nothing to do';
        }
        else{
            echo 'Done'; 
        }
    }
    else{
        die('Error : ('. $con->errno .') '. $con->error);
    }
    echo '<hr />';
    if (++$i == 5) break; 
}
?>

When the data is inserted into the database, the first record has always empty values, that effects the rest where their file names gets mixed up.

Is the issue with the loop? or shuffle? I tried using shuffle_assoc but gave me the same result. Any suggestion is much appreciated.




Aucun commentaire:

Enregistrer un commentaire