Hi all,

I am trying to connect to an FTP server and download a file. The problem I am having is I get the follwoing error:

Connected to vintageandclassic.com, for user redundget
Warning: ftp_get(LGW.CSV) [function.ftp-get]: failed to open stream: Permission denied in /var/www/vhosts/mydomaincom/httpdocs/lgw/vintageftp.php on line 29

Warning: ftp_get() [function.ftp-get]: Error opening LGW.CSV in /var/www/vhosts/mydomain.com/httpdocs/lgw/vintageftp.php on line 29

Can anyone see what I am doing wrong. Many thanks in advance.

$ftp_user_name = 'username'; 
$ftp_user_pass = 'password'; 


// set up basic connection 
$ftp_server = 'mydomain.com'; 

$conn_id = ftp_connect($ftp_server); 

$local_file = 'LGW.CSV';
$server_file = 'LGW.CSV';

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection 
if ((!$conn_id) || (!$login_result)) { 
echo "Ftp connection has failed!"; 
echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
die; 
} else { 
echo "Connected to $ftp_server, for user $ftp_user_name"; 
} 

// download the file 

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII )) {
   echo "Successfully written to $local_file\n";
} else {
   echo "There was a problem\n";
}


// close the FTP stream 
ftp_close($conn_id); 

    The error message pretty much spells it out; the FTP server refused to send PHP the file it requested because the user credentials apparently doesn't have permission to access that file.

    If you login to the same server using the same user credentials, can you download the file yourself (e.g. via a normal FTP client, not through PHP)?

      Hi Brad,

      and thank once again for your reply.

      Yes, if I use an FTP client or a browser I don't have a problem.

        Now that I re-read the error message, I see that it's somewhat ambiguous - it doesn't specify whether it was the remote or local stream that it failed to open. In other words, it's possible that PHP doesn't have permissions to write to the 'lgw' subdirectory.

        What are the permissions on this directory?

          Hi Brad,

          Your are spot on, I gave the directory "write" permissions and it works.

          Once again, thanks for all your help.

            Write a Reply...