1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
function taille($n){
if($n>1024){ return round(round($n/1024,1)/1024,1)." Mo"; }
else{ return round(round($n/1024,1),1)." Ko"; }
}
$fichier = $backup_name;
//verfie si fichier existe et supprime ancien fichier
if($opdossier = opendir($dossier))
{
while(false !== ($fichier = readdir($opdossier)))
{
if($fichier != '.' && $fichier != '..' && $fichier != 'dd.php' && $fichier != 'dd.html')
{
$explo = explode("_", $fichier);
$recdate = $explo[1];
if ($recdate < $datvieux){
//suppime fichier de + de 30 jours
unlink ($fichier);
} else if ($recdate == $datnow)
{
$tailfichier = taille(filesize($backup_name));
$tailfich = filesize($backup_name);
if ($tailfich < 24000000){
//recupère le fichier du jour et l'envoyer dans un mail en pièce jointe si taille inferieur à 24mo
$to = 'dd@yahoo.fr';
$from = 'dd@yahoo.fr';
$subject = 'Sav_Bd_dd';
$message_html = 'Sauvegarde de la base de données : dd <br />
Fichier : '.$backup_name.'<br />
Taille : '.$tailfichier;
$file_content = $backup_name;
$boundary_structure = md5(rand());
$boundary_alternatives = md5(rand());
$headers = [
"From: $from",
"Reply-To: $from",
"Content-Type: multipart/mixed; boundary=\"$boundary_structure\""
];
/* Encodage du contenu du fichier à envoyer. En accord avec la RFC 2045. */
$attachment = chunk_split(base64_encode($file_content));
/* Le mieux serait d'inclure un fichier de template mais pour l'exemple un ob_start fera l'affaire. */
ob_start();
?>
--<?=$boundary_structure . PHP_EOL /* Première partie de la structure: le message */?>
Content-Type: multipart/alternative; boundary="<?=$boundary_alternatives?>"
--<?=$boundary_alternatives . PHP_EOL /* Première alternative: plain text */?>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
<?=strip_tags($message_html)?>
--<?=$boundary_alternatives . PHP_EOL /* Seconde alternative: HTML */?>
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit
<?=$message_html?>
--<?=$boundary_alternatives /* Fin des alternatives */?>--
--<?=$boundary_structure . PHP_EOL /* Seconde partie de la structure: le fichier */?>
Content-Type: text/csv; name="nom_fichier.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?=$attachment?>
--<?=$boundary_structure /* Ferme la structure */?>--
<?php
$message = ob_get_clean();
$is_mail_sent = mail($to, $subject, $message, implode(PHP_EOL, $headers));
}
}
}
}
}
closedir($opdossier); |
Partager