How to Uploads from an open file and saves it to a file on the FTP server in PHP ?


To upload a file from an open file and save it to a file on the FTP server in PHP, you can use the FTP functions provided by PHP. Here are the steps to do it:

  • Connect to the FTP server using the ftp_connect() function. This function returns a resource that you can use to perform FTP operations.
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";

$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
  • Login to the FTP server using the ftp_login() function. This function takes the connection resource returned by ftp_connect(), the FTP username, and the FTP password as parameters.
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if (!$login_result) {
    die("Could not login to $ftp_server");
}
  • Set the transfer mode to binary using the ftp_set_option() function. This is necessary if you want to upload binary files like images or videos.
ftp_set_option($conn_id, FTP_BINARY, true);
  • Open the local file that you want to upload using the fopen() function. This function returns a file pointer that you can use to read the contents of the file.
$local_file = "/path/to/local/file.txt";
$fp = fopen($local_file, "r");
  • Upload the file to the FTP server using the ftp_fput() function. This function takes the connection resource returned by ftp_connect(), the remote file name, the file pointer returned by fopen(), and the transfer mode as parameters.
$remote_file = "/path/to/remote/file.txt";
$upload_result = ftp_fput($conn_id, $remote_file, $fp, FTP_BINARY);

if (!$upload_result) {
    die("Could not upload file to $ftp_server");
}
  • Close the file pointer and the FTP connection using the fclose() and ftp_close() functions.
fclose($fp);
ftp_close($conn_id);

Here's the complete code:

$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";

$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if (!$login_result) {
    die("Could not login to $ftp_server");
}

ftp_set_option($conn_id, FTP_BINARY, true);

$local_file = "/path/to/local/file.txt";
$fp = fopen($local_file, "r");

$remote_file = "/path/to/remote/file.txt";
$upload_result = ftp_fput($conn_id, $remote_file, $fp, FTP_BINARY);

if (!$upload_result) {
    die("Could not upload file to $ftp_server");
}

fclose($fp);
ftp_close($conn_id);

Alternatively, you can use the ftp_put() function instead of ftp_fput() to upload a file from a local path instead of an open file. Here's an example:

$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";

$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if (!$login_result) {
    die("Could not login to $ftp_server");
}

ftp_set_option($conn_id, FTP_BINARY, true);

$local_file = "/path/to/local/file.txt";

$remote_file = "/path/to/remote/file.txt";
$upload_result = ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY);

if (!$upload_result) {
    die("Could not upload file to $ftp_server");
}

ftp_close($conn_id);


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.