0

I have the following class for uploading files in a directory, however, the first directory that should be created is getting set up as 'File' rather then a 'File Folder'. Because of that, the items that should be uploaded are not uploading properly. I'm not sure if I am missing a step or not or if I set this up incorrectly.

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;
    }
}
Michael Gibson
  • 29
  • 1
  • 12

1 Answers1

0

It works for me. I tried $sftp->mkdir('Test folder') and $sftp->mkdir('./Test folder/Another folder'). The directories that were created were created as expected.

One thing to keep in mind... DIRECTORY_SEPARATOR is probably not the best thing to use in this instance. On Windows, for example, DIRECTORY_SEPARATOR is \ - not /. This is an issue because the SFTP specs say that directories are to be represented with / - not . Quoting draft-ietf-secsh-filexfer-02:

This protocol represents file names as strings. File names are assumed to use the slash ('/') character as a directory separator.

I guess to have further insight a copy of the SFTP logs would be helpful. Maybe just provide the SFTP logs of a mkdir operation. You can get the SFTP logs by doing define('NET_SFTP_LOGGING', 2) at the top and then doing echo $sftp->getSFTPLog() after the mkdir.

Community
  • 1
  • 1
neubert
  • 15,947
  • 24
  • 120
  • 212
  • Currently we are using a linux server but I will replace `DIRECTROY_SEPARATOR` with just a `'/'`. I noticed that you are using `'./...'` where the dot is in front of the first folder. I have my $dir set up as `'/home/...'` where home is a top level directory. Should that make a difference? – Michael Gibson Jul 15 '16 at 15:43
  • `$sftp->mkdir('Test folder/Sub folder')` worked for me as well so what you're doing should be a-ok. – neubert Jul 15 '16 at 15:45
  • I will have to continue doing some research then. For what ever reason, The directories are not being creating. I will trying the log and see what it says. I appreciate your help so far! – Michael Gibson Jul 15 '16 at 15:50
  • When you get the log either edit it into your orig post or copy / paste it to pastebin.org or some such and post the link. Thanks! – neubert Jul 15 '16 at 15:54
  • And apparently the entire time I had the server password wrong. I didn't get an error though, which seems odd. – Michael Gibson Jul 15 '16 at 16:10
  • Well I was able to get it to work using procedural code, but when I try to do it as a class, it doesn't seem to work correctly. I can't find any info as to the why that might be. – Michael Gibson Jul 15 '16 at 16:16
  • Maybe just get the SFTP logs for the class and then find the relevant parts of the log and share them. eg. look for `-> NET_SFTP_MKDIR` and the `<- NET_SFTP_STATUS` that follows it. – neubert Jul 15 '16 at 16:17