<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);	
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.1");


$handle=opendir ("../../web/intelectra_shop/documents/article/");
echo "Verzeichnisinhalt:<br>";
 $counter = array();
 $z = 0;
while ($datei = readdir ($handle)) {
$z++;
//if ($z < 50) {  
    //echo "$datei<br>";
    $number = substr($datei,0,6);
    // get item id
    $item_id = 0;
    $sql = "SELECT * FROM items WHERE number='".$number."'";
    $query2 = mysql_query($sql) or die("Anfrage nicht erfolgreich");
    while($obj2 = mysql_fetch_object($query2)) {
            $item_id = $obj2->id;
    }
    
    if ($item_id) {
            // set default image
            $sql = "UPDATE items
                    SET has_documents=1
                    WHERE id=".$item_id;
            $query = mysql_query($sql);
            if (!$query) {
                echo $sql;
                echo "<br />";
            }

        $size = filesize("../../web/intelectra_shop/documents/article/".$datei);
        $sql = "INSERT INTO item_files
            SET
                rang=0,
                type=1,
                file_name='".$datei."',
                item_id=".$item_id.",
                size='".$size."'";
        $query = mysql_query($sql);
        if (!$query) {
                echo $sql;
                echo "<br />";
        }
       
    }
}
//}
closedir($handle);


?>