dsu89430
dsu89430
2013-04-19 14:40

仅在文件存在时才创建目录(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 duankuang1046 8年前

    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.

    点赞 评论 复制链接分享
  • doumi1884 doumi1884 8年前
    // $csv = your checks for string ending to .csv
    $ftp_files = ftp_nlist($ftp_connection, ".");
    foreach ($ftp_files = $files) {
       if ($files = $csv) {
         // makedir
    

    maybe something like this in very basic syntax. ftp_nlist returns an array of all files in a particular directory.

    点赞 评论 复制链接分享