dsu89430 2013-04-19 14:40
浏览 355
已采纳

仅在文件存在时才创建目录(ftp mkdir)

I currently have a program that connects to an ftp directory, if it finds csv files, runs a script, then after the script has run on the files, it creates a back up folder with the date and moves the csv files to this newly created back up folder in the ftp directory.

However, if there are no csv files in the root directory, I do not want a backup folder to be created, as there are no files to move. I know the solution is probably really simple but I cannot seem to figure it out!

 logMessage("Creating backups");
    $ftp_connection = @ftp_connect($ftp_url, $ftp_port, 6000);
    if(!@ftp_login($ftp_connection, $ftp_username, $ftp_password )) {
       logMessage("Could not connect to FTP: [$ftp_url], with Username: [$ftp_username], and Password: [$ftp_password]");
        die();
    }

$date = date('Y_m_d_(His)');

$newBackup = $ftp_root."/".$ftp_backup."backup_$date";

if (ftp_mkdir($ftp_connection, $newBackup)) {
    logMessage ("Successfully created [$newBackup
]");

    foreach($filesToProcess as $file){
        $pathData = pathinfo($file);      
        if(isset($pathData['extension']) && $pathData['extension'] == 'csv'){

            if(!@ftp_rename($ftp_connection,
                    $ftp_root.'/'.$file,
                    $newBackup."/".$file)
            ){
                logMessage("Unable to move file: $file")
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • duankuang1046 2013-04-19 14:45
    关注

    You have used, foreach($filesToProcess as $file) ,so in $filesToProcess it's array of files. you can use, count($filesToProcess) first count number of files, then if count>0 execute code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?