Forked from steveosoule/php-ftp-file-upload-function.php
Created
September 14, 2020 14:35
-
-
Save josanua/f7abdf68739d858ac39e87441967bba8 to your computer and use it in GitHub Desktop.
PHP FTP File Upload Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$ftp_server = "ftp.domainname.com"; | |
$ftp_user_name = "ftp_username"; | |
$ftp_user_pass = "ftp_password"; | |
$ftp_directory = 'path/to/folder/'; // leave blank | |
$ftp_source_file_name = "data.xml"; | |
$ftp_dest_file_name = $ftp_source_file_name; | |
if( ftp_file( $ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_source_file_name, $ftp_directory, $ftp_dest_file_name) ){ | |
echo "Success: FTP'd data\n"; | |
} else { | |
echo "Error: Could not FTP data.\n"; | |
} | |
function ftp_file( $ftpservername, $ftpusername, $ftppassword, $ftpsourcefile, $ftpdirectory, $ftpdestinationfile ) | |
{ | |
$conn_id = ftp_connect($ftpservername); | |
if ( $conn_id == false ) | |
{ | |
echo "FTP open connection failed to $ftpservername \n" ; | |
return false; | |
} | |
$login_result = ftp_login($conn_id, $ftpusername, $ftppassword); | |
if ((!$conn_id) || (!$login_result)) { | |
echo "FTP connection has failed!\n"; | |
echo "Attempted to connect to " . $ftpservername . " for user " . $ftpusername . "\n"; | |
return false; | |
} else { | |
echo "Connected to " . $ftpservername . ", for user " . $ftpusername . "\n"; | |
} | |
if ( strlen( $ftpdirectory ) > 0 ) | |
{ | |
if (ftp_chdir($conn_id, $ftpdirectory )) { | |
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n"; | |
} else { | |
echo "Couldn't change directory on $ftpservername\n"; | |
return false; | |
} | |
} | |
ftp_pasv ( $conn_id, true ) ; | |
$upload = ftp_put( $conn_id, $ftpdestinationfile, $ftpsourcefile, FTP_ASCII ); | |
if (!$upload) { | |
echo "$ftpservername: FTP upload has failed!\n"; | |
return false; | |
} else { | |
echo "Uploaded " . $ftpsourcefile . " to " . $ftpservername . " as " . $ftpdestinationfile . "\n"; | |
} | |
ftp_close($conn_id); | |
return true; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment