php ftp制作多个目录

时间:2013-06-11 18:25:59

标签: php ftp makefile subdirectory

这是我的PHP代码。它实际上工作正常:

            $newftpdir_parent   = date('Ymd');                              // YYYYMMDD
            $newftpdir_child    = $ext_folder;                              // username
            $newftpdir          = $newftpdir_parent.'/'.$newftpdir_child;   // YYYYMMDD/username
            $conn_id            = ftp_connect($uptoftp_server);
            ftp_login($conn_id, $uptoftp_username, $uptoftp_pass);

            $path_of_storage    = $uptoftp_path_abs.'/'.$uptoftp_path_rel; // /var/www/uploads
            if( !@ftp_chdir($conn_id, $path_of_storage.'/'.$newftpdir_parent) ){
                  ftp_mkdir($conn_id, $path_of_storage.'/'.$newftpdir_parent);
                  ftp_chmod($conn_id, 0777, $path_of_storage.'/'.$newftpdir_parent);
            }
            if( !@ftp_chdir($conn_id, $path_of_storage.'/'.$newftpdir) ){
                  ftp_mkdir($conn_id, $path_of_storage.'/'.$newftpdir);
                  ftp_chmod($conn_id, 0777, $path_of_storage.'/'.$newftpdir);
            }

            ftp_chdir($conn_id, $path_of_storage.'/'.$newftpdir);

            ftp_put($conn_id, $file_name, $filetoftp, FTP_ASCII);

            ftp_close($conn_id);

它将文件从本地服务器上传到远程ftp服务器。你可以看到有两个丑陋的条件,检查目录是否存在,如果不是我们创建它并移入其中。再次为subdir。由于我现在希望我的目录(在ftp服务器上)看起来像YYYY / MM / DD /用户名而不是YYYYMMDD /用户名,我需要再创建两个这样的块。它工作得很好,但它看起来并不干净,我对它有点疯狂......我想知道是否有更好的方法来做到这一点,因为ftp_mkdir()不能制作多个目录(带有子目录的目录) )?

例如,只考虑/ uploads / dir存在,但不是/ 2013 / as subdir / 06 /和/ 11 /:

ftp_mkdir($conn_id, '/var/www/uploads/2013/06/11');

返回:

Warning: ftp_mkdir() [function.ftp-mkdir]: /var/www/uploads/2013/06/11: No such file or directory in /home/blabla/www/test.php on line 18

2 个答案:

答案 0 :(得分:5)

<?php

// function
function ftp_mksubdirs($ftpcon,$ftpbasedir,$ftpath){
   @ftp_chdir($ftpcon, $ftpbasedir); // /var/www/uploads
   $parts = array_filter(explode('/',$ftpath)); // 2013/06/11/username
   foreach($parts as $part){
      if(!@ftp_chdir($ftpcon, $part)){
         ftp_mkdir($ftpcon, $part);
         //ftp_chmod($ftpcon, 0775, $part);
         ftp_chdir($ftpcon, $part);
      }
   }
}

// usage
$path_of_storage = '/var/www/uploads';
$newftpdir = '2013/06/11/username';

$conn_id = ftp_connect($ftpserver);
ftp_login($conn_id, $login, $pass);
ftp_mksubdirs($conn_id,$path_of_storage,$newftpdir);
ftp_close($conn_id);

?>

答案 1 :(得分:1)

发现这一点,也许有帮助:

function ftp_mkdir_recusive($con_id,$path){
    $parts = explode("/",$path);
    $return = true;
    $fullpath = "";
    foreach($parts as $part){
            if(empty($part)){
                    $fullpath .= "/";
                    continue;
            }
            $fullpath .= $part."/";
            if(@ftp_chdir($con_id, $fullpath)){
               ftp_chdir($con_id, $fullpath);
            }else{
                    if(@ftp_mkdir($con_id, $part)){
                            ftp_chdir($con_id, $part);
                    }else{
                            $return = false;
                    }
            }
    }
    return $return;
}

$dir = "/2012/05/02/"

if(ftp_mkdir_recusive($con_id,$dir)){
     print("Successfully created $dir");
}else{
    print("There was a problem while creating $dir");
}