尝试下载文件时,PHP ftp_get无法打开流

时间:2014-01-11 16:25:54

标签: php ftp

我正在尝试让浏览器从FTP服务器下载文件,但无论我尝试什么,我都会收到此错误:

Warning: ftp_get(taak4.docx) [function.ftp-get]: failed to open stream: Permission denied in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231

Warning: ftp_get() [function.ftp-get]: Error opening taak4.docx in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231

然而,我确信我的FTP服务器工作正常,因为上传文件正常工作。我还将每个文件夹设置为chmod 777.有谁知道问题可能是什么?

我的PHP代码:

$local_file="taak4.dockx";
$server_file="taak4.dockx";
ftp_get($FTPClient->connectionId, $local_file, $server_file, FTP_BINARY);

提前致谢!

3 个答案:

答案 0 :(得分:1)

您必须指定文件的完整路径。例如:

/var/home/victor/files/taak4.dockx

使用$_SERVER['DOCUMENT_ROOT']获取文档根目录路径。

答案 1 :(得分:0)

您需要拥有$local_file路径的写入权限。让它成为一条完整的道路。示例:chmod 777 /test并将$local_file设为/test/taak4.docx

答案 2 :(得分:0)

即使更改了远程服务器上的文件权限,我也遇到了这个问题,我无法在本地服务器上下载它:

Warning: ftp_get(): Can't open Capture.PNG: No such file or directory in C:\MAMP\htdocs\ftp.php on line 25

解决方案:

在$ server_file变量中写入任何路径之前,必须包含'/',因此,可以正常使用的整个示例在此处:

// This is the path and new file name on my server (name can be different from the remote server file )
// if i want to save it just where my current php file is running ,no need to enter any path just file name
$local_file = 'capture.png';

// This is the path and file name on my Remote server from which i am downloading from
// this should start with '/' and write 'public_html' or 'htdocs' afterwards
$server_file = '/public_html/Capture.PNG';

// ftp details
$ftp_server="example.host.com";
$ftp_user_name="username";
$ftp_user_pass="password";

// 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);

// This is to so that we do not time out
ftp_pasv($conn_id, true);

// 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);