0

PHP 5.2.17 が実行されているサーバーで、組み込みの ftp ラッパーを使用してファイルをアップロードする関数を使用すると、サーバー上に空のファイルが作成されます。

  • file_put_contents()正確なバイト数を返します
  • copy()も true で返します

どちらもファイルを作成しますが、空です。

バイナリ モードと ascii モードの両方で FTP 拡張から試してみると、ftp_put()うまく機能します。

PHP 5.3.10 を搭載した私のワークステーションでは、何とかラッパーでも動作します。

コード内:

$source = '/tmp/testfile';
$target = 'ftp://user:pass@example.com/testfile';

copy($source, $target);

エラーや警告は表示されませんが、サーバーに空のファイルが残ります。

$source = '/tmp/testfile';
$target = 'testfile';

$ftp = ftp_connect('example.com');
ftp_login($ftp, 'user', 'pass');
ftp_put($ftp, $target, $source, FTP_ASCII);
ftp_close($ftp);

あらゆる点で機能します。

提案をありがとう!

4

1 に答える 1

-1

SSH2 ライブラリを試しましたか? 以下のいくつかのサンプル実装:

public function uploadSFTP($host_name, $port, $user, $publicSshKeyPath, $privateSshKeyPath, $remoteFile, $localFile, $fileOperation = 'w') 
{
    $ssh_conn = ssh2_connect($host_name, $port);

    if (ssh2_auth_pubkey_file($ssh_conn, $user, $publicSshKeyPath, $privateSshKeyPath)) 
    {
        $sftp_conn = ssh2_sftp($ssh_conn);
        $inputfileStream = @fopen('ssh2.sftp://' . $sftp_conn . $remoteFile, $fileOperation);

        try 
        {
            if (!$inputfileStream)
                throw new Exception('Could open remote file for writing: ' . $remoteFile);

            $localFileContents = @file_get_contents($localFile);

            if ($localFileContents === FALSE)
                throw new Exception('Could not open local file for reading :' . $localFile);

            if (@fwrite($inputfileStream, $localFileContents) === FALSE)
                throw new Exception('Could not SFTP file');
        } 
        catch (Exception $e) 
        {
            // Do something...
        }

        fclose($sftpInfileStream);
    }
}
于 2012-07-12T15:48:02.577 に答える