PHP: upload file to FTP server


How to upload file to FTP server using PHP?

<?php
 
$file = '/path/to/file/local';
$remote_file = 'file.remote';
$dir = '/path/to/local/dir';

$ftp_server = 'ftp_server.com';
$ftp_user = 'ftp_user';
$ftp_pass = 'ftp_pass';
 
$conn_id = ftp_connect($ftp_server);
 
ftp_login($conn_id, $ftp_user, $ftp_pass);
 
//very important 
ftp_pasv($conn_id, true);

ftp_chdir($conn_id, $dir); 

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}
 
ftp_close($conn_id);
?>

No comments yet.

Leave a Reply