使用Php将文件从FTP服务器复制到Cpanel

时间:2017-05-22 14:43:05

标签: php ftp-server

你能帮助我吗?我正在尝试将文件复制到应用程序内的文件夹中。该脚本正在我的本地机器上运行,但是当我在Cpanel服务器上运行时,它显示错误“ftp_login()期望参数1是资源,布尔给定”

这是我测试的脚本

    $folder_path = "192.xx.xx.xx\TMS"; 
    $local_file = "CurrentFile\Inbound.xls";
    $server_file = "CurrentFile\Inbound.xls";
    //-- Connection Settings
    $ftp_server = "192.xx.xx.xx"; // Address of FTP server.
    $ftp_user_name = "FTP server username"; // Username
    $ftp_user_pass = "FTP server Password"; // Password

    $target = 'CurrentFile';
    if (!file_exists($target)) 
    {
            die("Target directory doesn't exist.");
    }
    else if (!is_writable($target)) 
     {
            die("Insufficient privileges to write to the target directory!");
    }
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    // try to download $server_file and save to $local_file
    if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) 
    {
        echo "Successfully written to $local_file\n";
    }
    else 
    {
        echo "There was a problem\n";
    }

    function fileExists($path)
    {
        return (@fopen($path,"r")==true);
    }
    ftp_close($conn_id);

我认为脚本无法连接到FTP服务器。 如果我在创建连接后添加它,则返回“连接失败”。

 if(!$conn_id) {
die("Connection failed!");
}

1 个答案:

答案 0 :(得分:0)

我没有用过这个,但我建议你去看看这个教程ftp_login expects parameter 1 to be a resource,让我知道这个发生了什么