PHP的FTP使多个目录 [英] php ftp make multiple directories

查看:53
本文介绍了PHP的FTP使多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my PHP code. It actually works fine:

            $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服务器.如您所知,如果有条件检查目录是否存在,则有2个丑陋之处;如果不存在,我们将创建目录并将其移入目录.并再次为subdir.由于我现在希望我的目录(在ftp服务器上)看起来像YYYY/MM/DD/用户名而不是YYYYMMDD/用户名,因此我需要再创建两个if块.它工作得很好,但看起来并不干净,我对此有点疯狂……我想知道是否有更好的方法,因为ftp_mkdir()不能制作多个目录(目录中带有subdirs)?

It uploads a file from local server to remote ftp servers. As you can se there are 2 ugly if conditions that check if directory exists, if not we create it and move in it. And again for subdir. Since I now want my directories (on the ftp servers) to look like YYYY/MM/DD/username instead of YYYYMMDD/username, I will need to create two more of these if blocks. It works great, but it does not look clean, I'm kind of maniac about it... I was wondering if there is better way to do it, since ftp_mkdir() cannot make multiple dirs (dir with subdirs in it)?

例如,仅考虑/uploads/目录,但不考虑/2013/子目录/06/和/11/:

for exemple, considering only the /uploads/ dir exists, but not the /2013/ neither subdir /06/ and /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

推荐答案

<?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);

?>

这篇关于PHP的FTP使多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆