jdfauw
jdfauw

Reputation: 657

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:

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. I also set every folder to chmod 777. Does anyone know what the problem might be?

My php code:

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

Thanks in advance !

Upvotes: 4

Views: 15086

Answers (3)

MR_AMDEV
MR_AMDEV

Reputation: 1922

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:

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

Upvotes: 0

Kristian
Kristian

Reputation: 2261

You need to have write permission on the $local_file path. Make it a full path. Example: chmod 777 /test and make $local_file be like /test/taak4.docx.

Upvotes: 0

user3185506
user3185506

Reputation: 11

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.

Upvotes: 1

Related Questions