Memindahkan file ke lebih dari satu target directory

phpSudah lama sekali semenjak posting gw yang terakhir tentang Mengatasi MySql yang lama ketika koneksi pertama via LAN gw belum ngeblog lagi.

Sekarang gw pengen sharing tentang memindah file ke multiple target directory dengan PHP dan dijalankan dengan cron. Server yang gw gunakan adalah Ubuntu Server. Jadi gini, gw terima file-file xml yang di kirim dari kantor australia menggunakan ftp. Setelah file di terima, gw harus pindahkan file-filenya ke 2 lokasi baru tapi harus dalam server yang sama. File-file  xml yang berisi listingan properti itu nantinya akan diparsing dan diinsert ke database untuk digunakan oleh 2 website. Maka gw buatlah sebuah script sederhana tapi meringankan kerja gw.

Jadi langkah yang gw pakai pada tiap file adalah:

  1. Copy file ke lokasi 1
  2. Copy file ke lokasi 2
  3. Hapus file

Berikut ini scriptnya:

<?php
include_once "libs/logglib.php"; //optional, karena gw simpan lognya

$path_from = '/home/ftp/data/xml/';
$path_to = array();

//direktori tujuan untuk menempatkan file2 baru
$path_to[0] = '/opt/data/new/';
$path_to[1] = '/opt/data2/new/';
//tinggal di tambahkan target lainnya

$file_ext_allowed = array('xml');

$d = opendir($path_from);
$count = 0;
while(($f = readdir($d)) !== false)
  if(preg_match('/.xml/i', $f)) //pindah hanya jika filenya ber-extension xml
     ++$count;
closedir($d);
if($count==0){
    $logMsg = "no xml file in the path..";
    //karena saya perlu mencatat hasilnya ke log
    $Log = new logging($logMsg,"/opt/log/","move_feeds_log_",".log",2,true);
    echo die("no xml file in the path..\n");
}
$error = 0;
$msg = '';
if($h=opendir($path_from)){
    $x=0;
    while(false !== ($file=readdir($h))){
        $x++;
        if($file=='..' || $file=='.')continue;
        if(is_dir($file))continue;

        $file_str =  explode('.',$file);
        $ext = $file_str[ sizeof($file_str)-1 ];

        if(!in_array($ext,$file_str))continue;

        $old_file = $path_from.$file;
        if(is_dir($old_file))continue;

        foreach($path_to as $target){
            $new_file = $target.$file;
            echo "copying file $file to $target";
            if(copy($old_file,$new_file))
                echo "... ok\n";

            else{
                echo "... failed\n";
                $error = 1;
                $msg .= "Failed to move $old_file to $new_file \n";
            }
        }

        echo "deleting file $file ";
            if(unlink($old_file))
                echo "... ok\n";
            else
                echo "... failed\n";
    }

}

Semoga bermanfaat buat yang lain..

About these ads

2 thoughts on “Memindahkan file ke lebih dari satu target directory

  1. Pingback: Membuat Kamus menggunakan Google Translate API dengan PHP | gowar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s