/** * 拷贝或者移动文件 * * @param string $from_file 原始文件名 * @param string $to_file 拷贝到文件名,支持ftp模式,如 ftp://user:password@host/dir/file.txt * @param int $mode 保存后的文件权限 * @param bool $move 是否移动文件,默认false表示拷贝,true表示移动 * @access public * @return bool 成功返回true,失败返回false */ function jieqi_copyfile($from_file, $to_file, $mode = 0777, $move = false) { $from_file = trim($from_file); if (!is_file($from_file)) { return false; } $to_file = trim($to_file); $matches = array(); if (!preg_match('/^(ftps?):\\/\\/([^:\\/]+):([^:\\/]*)@([0-9a-z\\-\\.]+)(:(\\d+))?([0-9a-z_\\-\\/\\.]*)/is', $to_file, $matches)) { jieqi_checkdir(dirname($to_file), true); if (is_file($to_file)) { @unlink($to_file); } if ($move) { $ret = rename($from_file, $to_file); } else { $ret = copy($from_file, $to_file); } if ($ret && $mode) { @chmod($to_file, $mode); } return $ret; } else { include_once JIEQI_ROOT_PATH . '/lib/ftp/ftp.php'; $ftpssl = strtolower($matches[1]) == 'ftps' ? 1 : 0; $matches[6] = intval(trim($matches[6])); $ftpport = $matches[6] > 0 ? $matches[6] : 21; $ftp =& JieqiFTP::getInstance($matches[4], $matches[2], $matches[3], '.', $ftpport, 0, $ftpssl); if (!$ftp) { return false; } $matches[7] = trim($matches[7]); if (!$ftp->ftp_chdir(dirname($matches[7]))) { if (substr($matches[7], 0, 1) == '/') { $matches[7] = substr($matches[7], 1); } $pathary = explode('/', dirname($matches[7])); foreach ($pathary as $v) { $v = trim($v); if (strlen($v) > 0) { if ($ftp->ftp_mkdir($v) !== false && $mode) { $ftp->ftp_chmod($mode, $v); } $ftp->ftp_chdir($v); } } } $ret = $ftp->ftp_put(basename($matches[7]), $from_file); if ($ret && $mode) { $ftp->ftp_chmod($mode, basename($matches[7])); } //$ftp->ftp_close(); if ($move) { @unlink($from_file); } return $ret; } }
/** * 创建一个实例,如果已经存在则直接返回 * * @param string $ftphost ftp服务器地址 * @param string $ftpuser 用户名 * @param string $ftppass 密码 * @param string $ftppath 默认路径 * @param int $ftpport 端口号 * @access private * @return void */ function &getInstance($ftphost = '', $ftpuser = '', $ftppass = '', $ftppath = '.', $ftpport = 21, $timeout = 0, $ftpssl = 0, $ftppasv = 1) { $instance =& JieqiFTP::retInstance(); $inskey = md5($ftphost . ',' . $ftpuser . ',' . $ftppass . ',' . $ftppath . ',' . $ftpport . ',' . $timeout . ',' . $ftpssl . ',' . $ftppasv); if (!isset($instance[$inskey])) { $instance[$inskey] = new JieqiFTP($ftphost, $ftpuser, $ftppass, $ftppath, $ftpport, $timeout, $ftpssl, $ftppasv); $fid = $instance[$inskey]->ftp_connect(); if (!$fid) { return false; } } return $instance[$inskey]; }