Gå til innhold

Kjapp hjelp med php script


Anbefalte innlegg

Hola! Har funnet dette php scriptet på nettet, som sender e-post ved filendringer på en ftp. Men jeg vil modifisere det slik at i stedet for å sende e-post, viser den filendringene som tekst på en webside. Noen som kan hjelpe/guide meg i retning?

 

 

<?php
/**
* File : ftpMonitor.php
* Monitors a remote directory via FTP and emails a list of changes if any are
* found.
*
* @version June 4, 2008
* @author <a href="http://www.franzone.com">Jonathan Franzone</a>
*/

// Configuration ///////////////////////////////////////////////////////////////
$host = 'localhost';
$port = 21;
$user = 'anonymous';
$pass = '[email protected]';
$remote_dir = '/';
$cache_file = 'ftp_cache';
$email_notify = '[email protected]';
$email_from = '[email protected]';

// Main Run Program ////////////////////////////////////////////////////////////

// Connect to FTP Host
$conn = ftp_connect($host, $port) or die("Could not connect to {$host}\n");

// Login
if(ftp_login($conn, $user, $pass)) {

 // Retrieve File List
 $files = ftp_nlist($conn, $remote_dir);

 // Filter out . and .. listings
 $ftpFiles = array();
 foreach($files as $file)
 {
   $thisFile = basename($file);
   if($thisFile != '.' && $thisFile != '..') {
     $ftpFiles[] = $thisFile;
   }
 }

 // Retrieve the current listing from the cache file
 $currentFiles = array();
 if(file_exists($cache_file))
 {
   // Read contents of file
   $handle = fopen($cache_file, "r");
   if($handle)
   {
     $contents = fread($handle, filesize($cache_file));
     fclose($handle);

     // Unserialize the contents
     $currentFiles = unserialize($contents);
   }
 }

 // Sort arrays before comparison
 sort($currentFiles, SORT_STRING);
 sort($ftpFiles, SORT_STRING);

 // Perform an array diff to see if there are changes
 $diff = array_diff($ftpFiles, $currentFiles);
 if(count($diff) > 0)
 {
   // Email the changes
   $msg = "<html><head><title>ftpMonitor Changes</title></head><body>" .
     "<h1>ftpMonitor Found Changes:</h1><ul>";
   foreach($diff as $file)
   {
     $msg .= "<li>{$file}</li>";
   }
   $msg .= "</ul>";
   $msg .= '<em>Script by <a href="http://www.franzone.com">Jonathan Franzone</a></em>';
   $msg .= "</body></html>";

   $headers = "MIME-Version: 1.0\r\n";
   $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
   $headers .= "To: {$email_notify}\r\n";
   $headers .= "From: {$email_from}\r\n";
   $headers .= "X-Mailer: PHP/" . phpversion();

   mail($email_notify, "ftpMonitor Changes Found", $msg, $headers);
 }

 // Write new file list out to cache
 $handle = fopen($cache_file, "w");
 fwrite($handle, serialize($ftpFiles));
 fflush($handle);
 fclose($handle);
}
else {
 echo "Could not login to {$host}\n";
}

// Close Connection
ftp_close($conn);
?>

Endret av SaabAero
Lenke til kommentar
Videoannonse
Annonse

Kastet raskt sammen dette.

Modifiserte et guestbook script jeg fant her inne på diskusjon.no en gang.

 

<?php
/**
* File : ftpMonitor.php
* Monitors a remote directory via FTP and emails a list of changes if any are
* found.
*
* @version June 4, 2008
* @author <a href="http://www.franzone.com">Jonathan Franzone</a>
*/

// Configuration ///////////////////////////////////////////////////////////////
$host = 'localhost';
$port = 21;
$user = 'anonymous';
$pass = '[email protected]';
$remote_dir = '/';
$cache_file = 'ftp_cache';
$email_notify = '[email protected]';
$email_from = '[email protected]';

// Date
$date = time();

// Date format
define ('DATE_FORMAT', 'd.m.Y H:i:s');
$date = date(DATE_FORMAT, $date);

// Database
define ('ENTRIES_DB', "entries.db");

// Main Run Program ////////////////////////////////////////////////////////////

// Connect to FTP Host
$conn = ftp_connect($host, $port) or die("Could not connect to {$host}\n");

// Login
if(ftp_login($conn, $user, $pass)) {

// Retrieve File List
$files = ftp_nlist($conn, $remote_dir);

// Filter out . and .. listings
$ftpFiles = array();
foreach($files as $file) {
	$thisFile = basename($file);

	if($thisFile != '.' && $thisFile != '..') {
		$ftpFiles[] = $thisFile;
	}

}

// Retrieve the current listing from the cache file
$currentFiles = array();

if(file_exists($cache_file)) {
	// Read contents of file
	$handle = fopen($cache_file, "r");

	if($handle) {
		$contents = fread($handle, filesize($cache_file));
		fclose($handle);

		// Unserialize the contents
		$currentFiles = unserialize($contents);
	}
}

// Sort arrays before comparison
sort($currentFiles, SORT_STRING);
sort($ftpFiles, SORT_STRING);

// Perform an array diff to see if there are changes
 	$diff = array_diff($ftpFiles, $currentFiles);
 	if(count($diff) > 0) {

	// Email the changes
	$msg = "<html><head><title>ftpMonitor Changes</title></head><body>" .
     		"<strong>ftpMonitor Found Changes: ({$date})</strong><ul>";

	foreach($diff as $file) {
		$msg .= "<li>{$file}</li>";
	}

	$msg .= "</ul>";
	//$msg .= '<em>Script by <a href="http://www.franzone.com">Jonathan Franzone</a></em>';
	$msg .= "</body></html>";

	$headers = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
	$headers .= "To: {$email_notify}\r\n";
	$headers .= "From: {$email_from}\r\n";
	$headers .= "X-Mailer: PHP/" . phpversion();

	//mail($email_notify, "ftpMonitor Changes Found", $msg, $headers);
	$id = rand(1000, 9999);
	$date = time();
	$content = $id . '[{**--||--**}]' . $msg . '[{**--||--**}]' . $date . '[{**--||--**}]' . "\n";

	// Create if file does not exist
	if (!file_exists(ENTRIES_DB)) {
		$fp = fopen(ENTRIES_DB, 'a');
		fclose($fp);
	}

	if (!$fp = fopen(ENTRIES_DB, 'a')) {
		echo "<h2>Error!</h2><p>Could not open file.</p>";
		exit();
	}

	if (fwrite($fp, $content) === FALSE) {
		echo '<h2>Error!</h2><p>Could not write to the file.</p>';
		exit();
      		}

      		//echo '<h2>Success!</h2><br />';
      		fclose($fp);
}

// Write new file list out to cache
$handle = fopen($cache_file, "w");
fwrite($handle, serialize($ftpFiles));
fflush($handle);
fclose($handle);
}

else {
echo "Could not login to {$host}\n";
}

// Close Connection
ftp_close($conn);

// Read and output entries.db
$entries_array = array_reverse(file(ENTRIES_DB));

foreach ($entries_array AS $lines) {

$entry = explode('[{**--||--**}]', $lines);
$msg = $entry[1];
$date = $entry[2];
$replace = array('<br /><br />' => '</p><p>');
$msg = strtr($msg, $replace);

echo $msg;
echo '<hr />';
}

?>

 

Ser ut til å fungere sånn i første omgang...

Men har et stort utbedringspotensial :thumbup:

 

Eventuelt legge dette på slutten i en egen php fil "read_db.php".

// Database
define ('ENTRIES_DB', "entries.db");

// Read and output entries.db
$entries_array = array_reverse(file(ENTRIES_DB));

foreach ($entries_array AS $lines) {

$entry = explode('[{**--||--**}]', $lines);
$msg = $entry[1];
$date = $entry[2];
$replace = array('<br /><br />' => '</p><p>');
$msg = strtr($msg, $replace);

echo $msg;
echo '<hr />';
}

?>

Endret av sxxxe83
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...