$file_xml="modifica.xml";
// Controllo che il file sia stato inserito
if (file_exists($file_xml) ) {
 
    // Carica il file in una variabile $xml
    $xml = simplexml_load_file($file_xml);
     
    // Controllo tutti i tag "utenti"
    foreach( $xml->RECORD as $value ) {
     
        // Recupero i campi dalla tabell TDZ
    echo $codiceprodotto =$value->FDI_T218;
              $posizione =$value->FDI_T219;
               $img =$value->FDI_T438;
                   
                
                //tolgo estensione alla variabile img
                $file_name = basename($img, '.' . pathinfo($img)['extension']);
          
               $sqlfim= "INSERT INTO `image`(`id_image`, `id_product`, `position`, `cover`) VALUES ('','$codiceprodotto','$posizione','1')";
                 if (!$db->Query($sqlfim)) echo $success = false;  
//    echo $sqlfim ."<br>";
                    // estraggo id immagine
                 if (!$db->Query("SELECT * FROM image WHERE  id_product = '{$codiceprodotto}'"));    
                $imgestr = $db->Row();
                
             $sqlfsa= "INSERT INTO `image_shop`(`id_product`, `id_image`, `id_shop`, `cover`) VALUES ('$codiceprodotto','$imgestr->id_image','1','1')";
             if (!$db->Query($sqlfsa)) echo $success = false;  
             $sqlfsg= "INSERT INTO `image_lang`(`id_image`, `id_lang`, `legend`) VALUES ('$imgestr->id_image','1','$file_name')";
             if (!$db->Query($sqlfsg)) echo $success = false;  
//              echo $sqlfsg ."<br>";
        
             // creo le tabelle
             $folders_structure = implode('/', str_split($imgestr->id_image));
                var_dump($folders_structure);
                // metterlo tutto al contrario   
                $path="img/p/".$folders_structure;
                $dir = mkdir($path, 0777, true); 
                
                function download_remote_file($file_url, $save_to)
    {
                  
        $content = file_get_contents($file_url);
        file_put_contents($save_to, $content);
    }
        
         echo $file_url="http://linkesterno&tipodoc=Z&nomefile={$img}";
           echo $folders_structure2=$path."/";
           echo $content="/".$img;
           
       echo download_remote_file($file_url, realpath($folders_structure2) . $content);
        
        
        $file = $path."/$img";  
        $new_file = $path."/$imgestr->id_image-cart_default.jpg";  
if(copy($file, $new_file) === TRUE)  
   {  
//   echo 'File copiato';  
   }  
  
      $file2 = $path."/$img";  
        $new_file2 = $path."/$imgestr->id_image-home_default.jpg";  
if(copy($file2, $new_file2) === TRUE)  
   {  
//   echo 'File copiato';  
   }  
   
         $file3 = $path."/$img";  
         $new_file3 = $path."/$imgestr->id_image-large_default.jpg";  
if(copy($file3, $new_file3) === TRUE)  
   {  
//   echo 'File copiato';  
   }  
   
    $file4 = $path."/$img";  
        $new_file4 = $path."/$imgestr->id_image-small_default.jpg";  
if(copy($file4, $new_file4) === TRUE)  
   {  
//   echo 'File copiato';  
   } 
   
     $file5 = $path."/$img";  
        $new_file5 = $path."/$imgestr->id_image-thickbox_default.jpg";  
if(copy($file5, $new_file5) === TRUE)  
   {  
//   echo 'File copiato';  
   }  
     $file5 = $path."/$img";  
        $new_file5 = $path."/$imgestr->id_image.jpg";  
if(copy($file5, $new_file5) === TRUE)  
   {  
//   echo 'File copiato';  
   }  
   
    }
}else{
    echo "Nessun file inserito";
}