2010年4月10日 星期六

FTP server + PHP 新檔通知

先作一個遞迴列回資料夾和檔案名稱的程式:list.php
$today=system("date +%Y-%m-%d");
$yesterday=("date --date=\"1 day ago\" +%Y-%m-%d");


//getDirectory( "." );
getDirectory( "/var/lib/mldonkey/incoming/files" );



function getDirectory( $path = '.', $level = 0 ){

$ignore = array( 'cgi-bin', '.', '..' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.

$dh = @opendir( $path );
// Open the directory to the handle $dh

while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory

if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored

$spaces = str_repeat( '', ( $level * 4 ) );
// Just to add spacing to the list, to better
// show the directory tree.
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...

//echo "Directory:$spaces $file\n";
getDirectory( "$path/$file", ($level+1) );
// Re-call this same function but on a new directory.
// this is what makes function recursive.

} else {
echo "$spaces$file\n";
// Just print out the filename

}

}

}
closedir( $dh );
// Close the directory handle

}

?>

再來,做一個CheckNewFile.sh
#!/bin/sh
today=`date +%Y-%m-%d`
yesterday=`date --date="1 day ago" +%Y-%m-%d`
/usr/bin/php /root/list.php > /tmp/$today.log
/usr/bin/find /var/lib/mldonkey/incoming/files/ -name `diff -u /tmp/$yesteray.log \ /tmp/$today.log | grep +File: | awk -F: '{print $2}' ` | awk -F/files/ '{print $2}' > \ /tmp/$today-check.log
/usr/bin/test -s /tmp/$today-check.log && /usr/bin/php /root/MailCheckFile.php

如果今天的check.log是空的話,就不通知了。如果不是空的話,
再執行MailCheckFile.php(用PHPMailer改的程式),寄出通知信。

最後再做一件事,編輯crontab

0 8 * * * /bin/sh /root/CheckNewFile.sh

呼,這樣就好了。PHP+shell script是很強大的!

沒有留言:

張貼留言