This repository is an example of upload and download files via FTP server using PHP.
The primary purpose of an FTP server is to allow users to upload files.
Uploading and downloading files using PHP FTP functions is almost similar to doing it with an FTP client.
The ftp_connect(ftp_server) and ftp_login(ftp_connection, ftp_username, ftp_password) functions are used to establish the connection to the provided ftp host and log in with the ftp credentials.
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
After successful logon, can use the ftp_put() or ftp_get() functions.
The ftp_put() fuction transfers the local file to the remote directory on FTP server. The function returns true or false based on the upload process and the success or error message will be displayed accordingly.
The ftp_get() fuction transfers the remote file on FTP server to the local directory. The function returns true or false based on the download process and the success or error message will be displayed accordingly.
ftp_put(ftp_conn, remote_file, local_file, mode, startpos);
// OR
ftp_get(ftp_conn, local_file, remote_file, mode, startpos);
Once the file transfer is completed, the ftp stream will be closed using the ftp_close() function.
ftp_close($ftp_conn);
- 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.
- Create HTML form to upload file
- Define FTP settings
- Connect the FTP server
- Login the FTP server
- Upload or download file of FTP server
- Close the FTP server connection
🔴 Live
Last updated at 20 Mar 2023