在运行PHP 5.2.17的服务器上,使用任何使用内置ftp包装器上传文件的函数,在服务器上创建一个空文件:
file_put_contents()
返回准确的字节数copy()
也会返回true 两者都创建文件,但它是空的。
尝试使用FTP扩展程序中的ftp_put()
时,无论是二进制还是ascii模式,都可以正常运行。
在我的工作站上使用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);
在各方面都有效。
感谢任何建议!
答案 0 :(得分:-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);
}
}