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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配