我有以下用于在目录中上传文件的类,但是,应该创建的第一个目录被设置为“文件”而不是“文件夹”。因此,应该上传的项目没有正确上传。我不确定我是否遗漏了一个步骤,或者我是否设置不正确。
include 'Net/SFTP.php';
class SFTPConnection
{
private $sftp;
public function __construct($host, $username, $password)
{
$this->sftp = new Net_SFTP($host);
if (!$this->sftp)
error_log("Could not connect to $host.");
if (!$this->sftp->login($username, $password))
error_log("Could not authenticate with username $username " .
"and password $password.");
}
public function uploadFiles($dir, $to, $from) {
$result = array();
$toDir = str_replace($from, $to, $dir);
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
if(!$this->sftp->mkdir($toDir . DIRECTORY_SEPARATOR .$value)) {
echo "Could not create directory <br>'";
exit();
}
$result[$value] = $this->uploadFiles($dir . DIRECTORY_SEPARATOR . $value, $to, $from);
} else {
$result[] = $value;
echo "Uploading $dir/$value <br>";
if(!$this->sftp->put($toDir . DIRECTORY_SEPARATOR . $value, $dir . DIRECTORY_SEPARATOR . $value, NET_SFTP_LOCAL_FILE)) {
echo "Could not upload file <br>";
exit();
}
}
}
}
return $result;
}
}