Skip to content

Instantly share code, notes, and snippets.

@alexlatam
Created July 23, 2024 05:27
Show Gist options
  • Save alexlatam/2de64eaa779f3d83695046695f68c5d3 to your computer and use it in GitHub Desktop.
Save alexlatam/2de64eaa779f3d83695046695f68c5d3 to your computer and use it in GitHub Desktop.
FTP Connection using PHP
<?php
// Session data
$ftpHost = 'ftp.domain.ext';
$ftpUsername = 'username';
$ftpPassword = '*****';
// Connect with ftp host
$conn = ftp_connect($ftpHost) or die("Is not possible connect with ftp host");
// Login with ftp host
$ftpLogin = ftp_login($conn, $ftpUsername, $ftpPassword);
// SEND FILE TO FTP SERVER
$localFilePath = "index.php";
$remoteFilePath = "public/index.php";
if(ftp_put($conn, $remoteFilePath, $localFilePath, FTP_ASCII)) {
echo "File transferred successfully";
}
// GET FILE FROM FTP SERVER
if(ftp_get($conn, $localFilePath, $remoteFilePath, FTP_BINARY)) {
echo "File transferred successfully";
}
// DELETE FILE FROM FTP SERVER
if(ftp_delete($conn, $localFilePath, $remoteFilePath, FTP_BINARY)) {
echo "File transferred successfully";
}
// close FTP session
ftp_close($conn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment