FTP在同一台机器上传输文件?

时间:2015-07-16 18:23:27

标签: php ftp localhost

我正在尝试使用FTP在同一台计算机上传输文件。首先,我们可以在同一台机器上传输文件。如果是,我不明白使用ftp_put命令的用户名和密码是什么?我能够连接到localhost服务器,但我无法登录将文件从一个目录上传到其他目录。

function upload_file($con,$remote_path)
    {   
      $source_file = "testfile.txt";    
      $ret = ftp_put($con,$remote_path,$source_file,FTP_ASCII);
      while ($ret == FTP_MOREDATA) {
       echo "Uploading....";
       $ret = ftp_nb_continue($con);
        }
    return($ret);
     } 



    $host = "localhost";  //address of ftp server
       $username = ""; // Username
       $password = ""; //password
       $remote_path = "ftp://localhost/"; 
       $local_path = "/home/random/";

       try {
       $con = ftp_connect($host);
       echo $con."\n";
       if (false === $con) {
           throw new Exception('Unable to connect');
              }
           } 
       catch (Exception $e) {
          echo "Failure: " . $e->getMessage();}

      $loggedIn = ftp_login($con,$username,$password);
        if (true === $loggedIn) {
           echo 'Success!';
               }
             else {
              throw new Exception('Unable to log in');
              }

       $ret = upload_file($con,$remote_path);
       if ($ret != FTP_FINISHED) {
           echo "Warning!! Server not able to uplaod";
           exit(1);
            }

0 个答案:

没有答案