We can use php functions to do various ftp tasks on our server much like the tasks we do using an actual ftp program. Some of the functions include uploading files, downloading files and deleting files. These functions can sometimes save a user quite a bit of coding but can also cause some security risks.
In order to use these functions, we need to establish a connection to the server server. This is accomplished by using the ftp_connect() and ftp_login() functions. These are required at the beginning of your script or else we cannot do anything.
ftp_connect Establishes a connection to the ftp server. ftp_login Logs a user into the ftp server. ftp_close Closes the connection with the ftp server.
Here is an example script using each of the above:
CODE
<?php
// set up the settings $ftp_server = 'something.net'; $ftpuser = 'username'; $ftppass = 'pass';
// set up basic connection $conn_id = ftp_connect($ftp_server);
// login with username and password $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
// close the connection ftp_close($conn_id);
?>
This code doesnt really do anything excepts establishes a connection with the server, logs the user in and then terminates the connection.
Here is a list of some very useful functions:
ftp_pwd Returns current directory name ftp_cdup Changes to the parent directory ftp_chdir Changes directories on a FTP server ftp_mkdir Creates a directory ftp_rmdir Removes a directory ftp_nlist Lists the files within a given directory ftp_rawlist Returns a detailed list of files within a given directory ftp_get Downloads a file from the FTP server ftp_put Uploads a file to the FTP server ftp_size Returns the size of a given file ftp_rename Renames a file ftp_delete Deletes a file on the server ftp_quit Terminates the connection
To upload a file simply use:
CODE
<?php
// set up the settings $ftp_server = 'something.net'; $ftpuser = 'username'; $ftppass = 'pass';
// delete the below variables if you decide to use a form. $source_file = 'blah blah/blah.txt'; $destination_file = 'blah blah/blah.txt';
// set up basic connection $conn_id = ftp_connect($ftp_server);
// login with username and password $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // the ftp function
// close the connection ftp_close($conn_id);
?>
You can create a form with 2 input boxes named destination_file and source_file and have the submit button submit it to the above file which would allow you to create a quick and dirty upload form.