Pen Drive Content Copier with PHP: The Source Code

Nothing much to say. I am writing a PHP program that is intended to monitor for the availability of a directory and copy all data of that directory into another directory. It still needs huge improvements, testing and bug fixing. It was really fun coding it! :D

Feel free to play with the codes and drop me a few lines if you have something to say ! :D

Here’s the source code:

 
<?php
 
/*
 * Pen Drive Content Copier
 *
 * @Author Abu Ashraf Masnun
 * @Email  masnun@gmail.com
 * @URL    http://masnun.com
 *
 * @Description This script monitors for the availability of the
 * $source directory from time to time and copies all data into the
 * $target directory.
 *
 * //TODO Needs further improvements. Still in test phase. :(
 *
 */
 
 
 
$source = "/media/MASNUN"; // Source Directory -- Variable on Linux :-(
$target = "/home/masnun/copies"; // Target Directory
$sep = "/" ; // Directory Seperator
 
 
while(true) {
    scanMedia();
}
 
function scanMedia() {
    global $source, $target, $sep;
 
    if(is_dir($source)) {
        if(!file_exists($source.$sep."m17l40")) {
            full_copy( $source, $target );
            file_put_contents($source.$sep."m17l40","bWFzbnVuICYgbGFib255");
        }
    }
 
}
 
 
 
function full_copy( $source, $target ) {
    global $sep;
    if ( is_dir( $source ) ) {
        @mkdir( $target );
        $d = dir( $source );
        while ( FALSE !== ( $entry = $d->read() ) ) {
            if ( $entry == '.' || $entry == '..' ) {
                continue;
            }
            $Entry = $source . $sep . $entry;
            if ( is_dir( $Entry ) ) {
                full_copy( $Entry, $target . $sep . $entry );
                continue;
            }
            copy( $Entry, $target . $sep . $entry );
        }
 
        $d->close();
    }else {
        copy( $source, $target );
    }
}
 
 
if(!function_exists("file_put_contents")) {
 
    function file_put_contents($source,$data) {
        $fh = fopen($source,"w");
        return fwrite($fh,$data);
 
    }
 
 
}
 
?>
Tags: . Bookmark the permalink.

3 Responses to Pen Drive Content Copier with PHP: The Source Code

  1. Shabbir says:

    Well i am just too naive to use the program.

    However, enlighten me informing the way to make it useful.

    Cheers !

  2. tom.higgy says:

    I think this would be better (and shorter) as a bash script but anyway, I’d suggest using sleep() in your while loop so it isn’t constantly polling.

    Interesting idea, though!

  3. Pingback: Tweets that mention Pen Drive Content Copier with PHP: The Source Code | maSnun.com -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">