Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active October 14, 2020 06:31
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save steveosoule/7592774 to your computer and use it in GitHub Desktop.
Save steveosoule/7592774 to your computer and use it in GitHub Desktop.
PHP FTP File Upload Function
<?
$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;
}
?>
@alex4buba
Copy link

This script is the server side, can someone point me to the HTML script / client side please?
The PHP script has no access to my local Windows drive C:\

Cheers

@parsibox
Copy link

parsibox commented May 2, 2019

html is not a programing language
you can not do it with html
you can use c# , python ,java ,... to do this

@derrickwaweru
Copy link

This code works pretty well thanks

For the html it's just a simple form to enable the user choose a file to upload through a submit/upload button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment