开发者

Cannot download to Hard Drive using ftp_get()

Is it possible to download to my Hard Drive (as opposted to my remote server) using PHP's ftp_get? The download using ftp_get() is successful, however, the file that I downloaded from my remote server is being downloaded to the directory of my php script. I'm not surprised by any means,but I would like to know how I can change the download directory to a specific location on my hard drive - say, the "C:\" drive for example.

The below code was take from php.net but this is exactly how my code is setup as well:

<?php

// define some variables
$local_file = 'local.rar';
$server_file = 'server.rar';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with us开发者_如何学编程ername 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";
}

// close the connection
ftp_close($conn_id);

?>

Thank you for any help,

Evan


To download the file locally, your PHP script needs to send the appropriate headers and then echo out the file's contents. However, this can only happen if you have not yet caused any other output from the PHP script (via echo or otherwise). This code should cause your browser to open a file save window or download it to the default location.

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {

    // Don't echo output here...
    //echo "Successfully written to $local_file\n";

    // You've downloaded the file into `$local_file` on your server. 
    // Now send it to the browser:
    header("Content-type: application/x-rar-compressed");

    // Also helps to send Content-length
    header("Content-length: " . filesize($local_file));

    // Dump out the file contents
    echo file_get_contents($local_file);

    // Delete it from the server
    unlink($local_file);

    // Always exit when you're done
    exit();
} else {
    echo "There was a problem\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜