如何从PHP上传SFTP文件(How to SFTP upload files from PHP)
我在使用PHP来SFTP上传文件到远程服务器时遇到了麻烦。 当我使用cURL时,我收到此处描述的错误:
来自PHP的SFTP - 未定义的常量CURLOPT_PROTOCOLS和CURLPROTO_SFTP?
我也按照以下建议尝试了phpseclib:
但是,当我尝试phpseclib时,我得到这些错误:
Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53 Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53
然后我尝试在php中使用系统命令,但没有任何反应:
<?php echo system('sftp user@ftp.domain.com'); echo system('password'); echo system('put file.csv'); ?>
我也试过了
<?php $connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644); ?>
但是我的php服务器说没有定义ss2_connect。
我试图从终端做以下事情
scp file.csv user@ftp.remote.com password
但是服务器不允许使用scp命令。 我没有shell访问权限来创建ssh密钥。
我现在能做的就是从终端sftp并手动上传。 但我真的想自动化这个,所以PHP网站可以做到这一切。
关于如何从PHP上传SFTP的教程不多。 是因为这是一件坏事吗? 如果是这样,我该怎么办? 我要上传的服务器只允许sftp连接。
I'm having trouble using PHP to SFTP upload files to a remote server. When I use cURL, I'm getting the error described here:
SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?
I also tried phpseclib as suggested in:
But when i try phpseclib, i get these errors:
Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53 Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53
I then tried using system commands in php like so, but nothing happened:
<?php echo system('sftp user@ftp.domain.com'); echo system('password'); echo system('put file.csv'); ?>
I also tried
<?php $connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644); ?>
but my php server says ss2_connect is not defined.
I tried to do the following from terminal
scp file.csv user@ftp.remote.com password
But the server does not allow scp command. I do not have shell access to create ssh keys.
All i can do right now is sftp from terminal and manually upload. But I really want to automate this so a PHP website can do all this.
There aren't many tutorials on how to SFTP upload from PHP. Is it because it's a bad thing to do? If so, what should I be doing? The server I want to upload to only allows sftp connections.
原文:https://stackoverflow.com/questions/1767117