dongwubao4785
dongwubao4785
2012-03-19 07:48

将文件发送到linux服务器

已采纳

I'm kind of new to php, and I'm trying to send a file through ftp to a computer that's runs "Centos 6" (Linux server). My code is :

<?php
$ftp_server = "XX.XXX.XXX.XXX";
$ftp_user = "user";
$ftp_pass = "password";

//set up a connection or die
 $conn_id = ftp_connect($ftp_server,22) or die("Couldn't connect to $ftp_server"); 
 ftp_pasv($conn_id, true);

 //try to login
 if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server
";
 } else {
    echo "Couldn't connect as $ftp_user
";
 }

  //close the connection
 ftp_close($conn_id);  
?>

I'm running the latest versions of Mysql and Apache, and also disable the firewall in the server. I can get to the server with Filezilla but only if I use port 22.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongyan1974 dongyan1974 9年前

    This is how i did it:

    <?php
    include('Net/SFTP.php');
    
    $sftp = new Net_SFTP('xx.xxx.xxx.xxx');
    if (!$sftp->login('user', 'pass')) {
        exit('Login Failed');
    }
    
    //Write to a text file
    $sftp->put('destinationInServer/filename', file_get_contents('Source/filename'));
    echo "Success";
    ?>
    
    点赞 评论 复制链接分享
  • dtoqa66028 dtoqa66028 9年前

    The problem is, you are trying to use FTP protocol to connect to a server that only responds to SFTP (port 22 is SSH and it essentially provides file transfer capabilities), but it is a different protocol altogether.

    Try looking at ssh2 functions instead: http://php.net/manual/en/function.ssh2-sftp.php

    点赞 评论 复制链接分享

为你推荐