How To Upload File On The FTP Server Using PHP
In this article, we will see how to upload files on the FTP server using PHP. As we know there are many FTP functions in PHP but whenever you want to upload a file in FTP using PHP that time FTP put function is very useful. The primary purpose of an FTP server is to allow users to upload and download files.
So, let's see FTP function, file upload in FTP, ftp_put function, how to upload file to FTP server in Linux, upload files on FTP server using laravel, laravel upload file to the remote server.
The ftp_put() function is an inbuilt function in PHP that is used to upload files to the FTP server.
ftp_put(ftp_conn, remote_file, local_file, mode, startpos);
ftp_conn - ftp_conn is a required parameter and it is used to specify the FTP connection.
remote_file - remote_file is a required parameter and it is used to specify the file path to the upload path.
local_file - local_file is a required parameter and it is used to specify the path of the file to upload.
mode - mode is an optional parameter and it is used to specify the transfer mode. It has 2 possible values: 1) FTP_ASCII 2) FTP_BINARY.
startpos - startpos is an optional parameter and it is used to specify the position in the remote file to start uploading to.
Now, we will see the upload local file to a file on the FTP server:
// connect to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
//login to FTP server
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
echo "Successfully uploaded $file.";
echo "Error uploading $file.";
// close connection
You might also like :