使用ftp_get下载的文件会损坏/损坏或者太大

时间:2014-12-10 17:50:30

标签: php ftp download

我使用php从服务器下载文件并保存到用户的电脑,但是当我尝试打开它们时,我无法,因为它说文件已损坏/损坏/太大,取决于文件类型。

if(isset($_REQUEST['download']))    
{

    // connect and login to FTP server
    $ftp_server = "**************.**********.***";
    $ftp_username = "a******";
    $ftp_userpass = "******";

    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

    //echo "curr: ".ftp_pwd($ftp_conn) . "\n";

    if (ftp_chdir($ftp_conn, "public_html/files")) {
        //echo "Current directory is now: " . ftp_pwd($ftp_conn) . "\n";
    } else { 
        echo "Couldn't change directory\n";
    }


    if(isset($_POST['checkbox']))
    {
        foreach($_POST['checkbox'] as $selected)
        {
            //echo $selected;

            $local_file = "local.zip";
            $server_file = "$selected";

            // download server file
            if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
            {
                //header("Content-type: application/local.zip");
                header("Content-length: " . filesize($local_file));
                header("Content-disposition: attachment; filename= $selected");

                //echo file_get_contents($local_file);

                unlink($local_file);

                exit();
                //echo "Successfully written to $local_file.";
            }
            else
            {
                echo "Error downloading $server_file.";
            }
        }
    }
    // close connection
    ftp_close($ftp_conn);
}

它正在做我想做的事,但想知道我是否做错了为什么它没有按预期工作。

由于

0 个答案:

没有答案