W3docs

ftp_put()

The ftp_put() function is a built-in PHP function that uploads a file to an FTP server. In this article, we'll discuss the function in detail and provide you

Understanding the PHP Function ftp_put()

The ftp_put() function is a built-in PHP function that uploads a file to an FTP server. In this article, we'll discuss the function in detail and provide you with a comprehensive guide to using it in your PHP projects.

What is ftp_put()?

The ftp_put() function uploads a local file to a remote FTP server. It accepts the following parameters:

  1. $ftp_stream: The connection identifier returned by the ftp_connect() function.
  2. $remote_file: The remote file path on the FTP server.
  3. $local_file: The local file path on the local machine.

The function returns true on success. Otherwise, it returns false.

Syntax of ftp_put()

The syntax of the ftp_put() function is as follows:

Syntax of ftp_put()

bool ftp_put ( $ftp_stream , string $remote_file , string $local_file , int $mode = FTP_BINARY )

The ftp_put() function takes four parameters, where the $mode parameter is optional. The $ftp_stream parameter is the connection identifier returned by the ftp_connect() function. The $remote_file parameter is the remote file path on the FTP server. The $local_file parameter is the local file path on the local machine. The $mode parameter specifies the transfer mode, which defaults to FTP_BINARY.

Usage of ftp_put()

To use the ftp_put() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:

Usage of ftp_put()

<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');

// Login with your FTP credentials
ftp_login($conn, 'username', 'password');

// Upload a local file to the remote server using ASCII mode
ftp_put($conn, '/public_html/index.html', '/local/index.html', FTP_ASCII);

// Close the FTP connection
ftp_close($conn);

In this example, we establish a connection to the FTP server using the ftp_connect() function. Then we log in using our FTP credentials using the ftp_login() function. We upload a local file to the remote server using the ftp_put() function with the FTP_ASCII transfer mode, and close the FTP connection using the ftp_close() function.

Error handling in ftp_put()

It's important to handle errors properly when using the ftp_put() function. If the function returns false, it means that the upload was unsuccessful. Here's an example of how to handle errors:

Error handling in ftp_put()

<?php

$file_uploaded = ftp_put($conn, '/public_html/index.html', '/local/index.html', FTP_BINARY);

if (!$file_uploaded) {
    echo "Failed to upload file to remote server.\n";
}

ftp_close($conn);

By handling errors appropriately and checking the return value of the function, you can ensure the success of your FTP operations using the ftp_put() function.

Conclusion

In conclusion, the ftp_put() function is a useful tool for uploading files to an FTP server. With proper usage and error handling, this function can be a valuable asset in your PHP projects.

Practice

Practice

What is the function of ftp_put() in PHP?