FTP File access using PHP
With PHP, there’s always more than one way to accomplish a particular task. Take file upload, for example. Sure, you can do it the traditional way, using HTTP file upload and transferring the file directly to a location on your Web server’s disk. Or you can do it the more exotic way, and use the FTP protocol to upload in a two-step process: from your local disk to a Web server, and then to an FTP server.
PHP supports both FTP and HTTP upload methods natively, leaving it to you to make the best decision based on the design requirements of your application. Transferring a file using PHP’s FTP functions is almost the same as doing it using a traditional FTP client–as you’ll see even the function names are similar to standard FTP commands.
The ftp_connect() and ftp_login() functions are used to initiate a connection to the named FTP host, and log in to it using the supplied credentials.
Assuming a successful login, the ftp_put() function is used to upload the file from the working directory to the remote directory specified by the user and rename it to its original form. Note the addition of the special FTP_BINARY argument to ftp_put(), to specify that the file be transferred in binary (not ASCII) mode. Depending on the result code returned by the ftp_put() function, an error or success message is displayed to the user.
The ftp_get() function is used download a server file and save it locally. This function will take four parameters first is the connection ID, next is the server filename , next is the local file name and location and lastly the file transfer mode whether it is binary or ascii.
The ftp_close() function is used to end the FTP session, and the unlink() function is used to clean up by deleting the local copy of the file that was created in step (2).
