W3docs

ftp_fput()

The ftp_fput() function is a PHP built-in function that uploads a file to the FTP server. The function takes three parameters:

What is ftp_fput()?

The ftp_fput() function is a PHP built-in function that uploads a file to the FTP server. The function takes five parameters:

  1. ftp_stream: The connection identifier returned by the ftp_connect() function.
  2. remote_file: The remote file name to which the file should be uploaded.
  3. local_file: A file handle resource opened with fopen() that points to the local file to be uploaded.
  4. mode (optional): The transfer mode (FTP_ASCII or FTP_BINARY). Defaults to FTP_ASCII.
  5. startpos (optional): The starting position in the remote file for the upload. Defaults to 0.

Unlike ftp_put(), which accepts a local file path string, ftp_fput() requires an open file handle resource.

The function returns a boolean value. If the function is successful in uploading the file, it returns true. Otherwise, it returns false.

Syntax of ftp_fput()

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

Syntax of ftp_fput()

bool ftp_fput ( resource $ftp_stream , string $remote_file , resource $local_file [, int $mode = FTP_ASCII [, int $startpos = 0 ]] )

Usage of ftp_fput()

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

Usage of ftp_fput() in PHP

<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
if (!$conn) {
    die("Could not connect to FTP server.");
}

// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
    die("Login failed.");
}

// Open the local file
$local_file = fopen('/local/directory/local_file.txt', 'r');
if (!$local_file) {
    die("Could not open local file.");
}

// Upload the file to the remote FTP server
if (ftp_fput($conn, 'remote_file.txt', $local_file, FTP_ASCII)) {
    echo "File uploaded successfully.\n";
} else {
    echo "Failed to upload the file.\n";
}

// Close the file handle and FTP connection
fclose($local_file);
ftp_close($conn);

In this example, we establish a connection to the FTP server using the ftp_connect() function and verify it succeeded. Then we log in using our FTP credentials with ftp_login() and check for errors. Next, we open the local file with fopen() to get the required file handle. Finally, we upload the file to the remote FTP server using ftp_fput() and close both the file handle and the FTP connection.

Error handling in ftp_fput()

It's important to handle errors properly when using the ftp_fput() function. If the function returns false, it means that the file couldn't be uploaded for some reason. Here's an example of how to handle errors:

Error handling in ftp_fput()

<?php

// Assuming $conn is already established via ftp_connect() and ftp_login()
$local_file = fopen('/local/directory/local_file.txt', 'r');
if ($local_file && ftp_fput($conn, 'remote_file.txt', $local_file, FTP_ASCII) === false) {
    echo "Failed to upload the file.\n";
} else {
    echo "File uploaded successfully.\n";
}
fclose($local_file);

In this example, we check the return value of the ftp_fput() function. If it's false, we display an error message; otherwise, we display a success message. Always remember to close the file handle with fclose() after the operation.

Conclusion

The ftp_fput() function is a useful PHP built-in function that allows you to upload a file to an FTP server. By following the guidelines and best practices outlined in this article, you can use the ftp_fput() function in your PHP projects with confidence.

Practice

Practice

What does the fput function do in PHP?