In the first part of this tutorial we have seen how to connect to FTP using PHP, upload a file and list the content from a specified directory.
In the second part we will cover the following :
- how to copy files from the FTP server using PHP.
- how to create a new directory on FTP server.
- transfer just a part of a file.
- other functions useful for FTP.
For copying a file from the FTP server we can use ftp_get(conn_id, client, file, transfer_type). Is similar with ftp_put().
If there exists another file with the same name on client side, will be overwritten.
We can make a new directory on FTP server using ftp_mkdir(conn_id, dir_name). For deleting the directory we can use ftp_rmdir(conn_id, dir_name). Note that PHP must have write access on the server.
PHP offers the possibility to transfer just a part of the file. We can do this using ftp_fput(conn_id, destionation, file_pointer, transfer_type).
file_pointer – is the pointer of the file opened with fopen(). It retains the content of this file from this pointer to the end of the file.
Other functions for FTP inside PHP:
a) ftp_cdup(conn_id) changes the current directory to the main directory.
b) ftp_chmod(conn_id, octal_CHMOD, file) – change the rights of a file.
c) ftp_pwd(conn_id) – returns the current working directory.
d) ftp_rename(conn_id, current_name, new_name) – renames the file or the directory.
e) ftp_size (conn_id, file) – returns the size in bytes of the file; Note: some servers do no support this function.
That’s it! I hope you liked this PHP tutorial that showed you how to use FTP functions inside PHP.