dongzice4895 2014-12-11 13:18
浏览 45
已采纳

如何使用php从另一台服务器打开txt文件

I want to open and write a txt file from another server, but I don't know how to do it? Can anyone help me?

<?PHP
   $fname=$_POST["fname"];

   $groupid=$_POST["groupid"];
   $myfile = fopen("Ji.txt", "a+") or die("Unable to open file!");
   fwrite($myfile, $fname."|".$groupid."
");
   fclose($myfile)
?>

I want to replace Ji.txt with http://xxx.xxx.xx.x/ddd.txt

</div>
  • 写回答

5条回答 默认 最新

  • douju2012 2014-12-11 13:39
    关注

    Alternatively you can do it with the FTP functions

    <?php
    // Connect to remote FTP server
    $conn = ftp_connect("ftp.example.com")or die("Cant connect to ftp server");
    $login = ftp_login($conn, "username", "password");
    
    // Open local (temporary) file handle
    $fh = fopen("Ji.txt", "a+");
    
    // Get remote file and save it to the previous file handle
    if(ftp_fget($conn, $fh, "Ji.txt", FTP_ASCII))
    {
        // Local file has now been updated with the content of the remote Ji.txt
        $fname = $_POST['fname'];
        $groupid = $_POST['groupid'];
        fwrite($fh, $fname.'|'.$groupid.'
    ');
        if(ftp_fput($conn, "Ji.txt", $fh, FTP_ASCII))
            echo 'File saved to remote server';
        else
            echo 'Error saving to remote server';
    }
    else
        echo 'Error downloading remote file';
    
    ftp_close($conn);
    fclose($fh);
    ?>
    

    Read more about ftp_fput etc here: http://php.net/manual/en/function.ftp-fput.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?