简体   繁体   English

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

[英]PHP ftp_get failed to open stream when trying to download file

I'm trying to make the browser download a file from an FTP server, but whatever I try, I'm getting this error: 我正在尝试让浏览器从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

I am however 100% sure my FTP server is working fine, as uploading files works correctly. 但是我100%确定我的FTP服务器工作正常,因为上传文件正常工作。 I also set every folder to chmod 777. Does anyone know what the problem might be? 我还将每个文件夹设置为chmod 777.有谁知道问题可能是什么?

My php code: 我的PHP代码:

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

Thanks in advance ! 提前致谢 !

you must specify the full path to the file. 您必须指定文件的完整路径。 For example: 例如:

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

Use $_SERVER['DOCUMENT_ROOT'] for get document root dir path. 使用$_SERVER['DOCUMENT_ROOT']获取文档根目录路径。

You need to have write permission on the $local_file path. 您需要对$local_file路径具有写权限。 Make it a full path. 让它成为一条完整的道路。 Example: chmod 777 /test and make $local_file be like /test/taak4.docx . 示例: chmod 777 /test和make $local_file类似于/test/taak4.docx

I was also suffering from this issue even after changing the file permissions on my remote server i was not able to download it on my local server: 即使在我的远程服务器上更改文件权限后,我也遇到了这个问题,我无法在本地服务器上下载它:

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

SOLUTION: 解:

One must include '/' before writing any path in the $server_file variable so the whole example that works just perfect is here: 在$ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM