是否可以使用PHP的到达下载到我的硬盘驱动器(如我的远程服务器所示)?使用ftp_get()下载是成功的,但是,我从远程服务器下载的文件将被下载到我的php脚本的目录中。我对任何方法都不感到惊讶,但我想知道如何将下载目录更改为硬盘上的特定位置,例如"C:\“驱动器。
下面的代码来自php.net,但这正是我的代码的设置方式:
<?php
// define some variables
$local_file = 'local.rar';
$server_file = 'server.rar';
// 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";
}
// close the connection
ftp_close($conn_id);
?>谢谢你的帮助,
埃文
发布于 2011-09-24 15:49:05
要在本地下载文件,PHP脚本需要发送适当的头,然后回显文件的内容。但是,只有在尚未从PHP脚本(通过echo或其他方式)引发任何其他输出的情况下,才会发生这种情况。此代码应导致浏览器打开文件保存窗口或将其下载到默认位置。
// 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";
}https://stackoverflow.com/questions/7540031
复制相似问题