PHPを使ってFTPでNAS上のファイルを削除したいと考えています。
条件として
1.NAS上のファイルを作成日から指定期間経過した物はを削除する
2.ファイル名等で予め指定したファイルは、その期間経過後も削除しない
上記サイト通り行うと1は問題なくクリアするのですが、2が実現できません。
記載されている内容からすると2も出来るようなのですがスルーしているようです。
これはどのようにすれば実現できますでしょうか。
あと33行目がそのままだと実行時にエラーが表示されます。
これを
if(!ftp_delete
↓
if(!@ftp_delete
このように修正するとエラーも止まったのですが、この意味は何なのでしょうか。
以上、よろしくお願いいたします。
<?php $host = "backups.mydomain.com"; //Replace with your host $username = "myUsername"; //Replace with your username $password = "myPassword"; //Replace with your password $mode = "passive"; //Leave blank to go to active mode $dir = "/"; //Put the name of the directory in here where you want to loop through files, put / for root directory $daysOld = '2'; //Enter the age of files in days, if a file should be deleted that's older than 2 days enter 2 $filesToSkip = array('.','..','.ftpquota'); //Contains the files that the script needs to skip if it comes accross them $notificationEmail = 'notifyt@mydomain.com'; //The email address to send a notification when file fails to delete //FTP session starting $connection = ftp_connect($host); $login = ftp_login($connection,$username,$password); if(!$connection || !$login){ die('Connection attempt failed!'); } if($mode == 'passive'){ //Switching to passive mode ftp_pasv($connection,TRUE); }else{ ftp_pasv($connection,FALSE); } //Calcuting the datetime of todays day minus the amount of 2days entered $dateToCompare = date('Y-m-d', strtotime('-'.$daysOld.' days',time())); //Looping through the contents of the provided directory $files = ftp_nlist($connection,$dir); //ftp_rawlist — Returns a detailed list of files in the given directory foreach($files as $file){ //Check if the file is in the list of files to skip, if it is we continue the loop if(in_array($file, $filesToSkip)){ continue; } $modTime = ftp_mdtm($connection, $file); if(strtotime($dateToCompare) >= $modTime){ if(!ftp_delete($connection,$file)){ //Deleting the file that needs to be deleted //If the file fails to delete we send a mail to the administrator mail($notificationEmail, 'FAILED TO DELETE FILE', 'FAILED TO DELETE FILE: '.$file); } } } ?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/03/20 08:18