Upload to FTP with PHP

$fp = fopen('https://www.example.com/pdfdoc', 'r');

$user = "sammy";
$pass = "password";
$ftp_server = "192.168.10.10";

//should be wrapped in try catch to properly handle errors
$ftp_conn = ftp_ssl_connect($ftp_server);
$login = ftp_login($ftp_conn, $user, $pass);

ftp_chdir($ftp_conn, 'path/to/folder'); //can also use ftp_pwd 

ftp_pasv($ftp_conn, true); //passive mode

ftp_fput($ftp_conn, "mydocument.pdf", $fp, FTP_BINARY);

fclose($fp);
ftp_close($ftp_conn);

Above code can be used to upload a file to an ftp server.  ftp_fput function requires a stream.  It can be open from a remote server or locally using a wrapper

Depending on ftp server connection can be established with ftp_connect or ftp_ssl_connect.  If data connection needs to be initiated by client ftp_pasv is used to set passive mode.  Of course, some lines of the code need to be wrapped in try…catch blocks to properly handle errors and close connections.

Share this article

Posted

in

by

Tags: