This is a discussion on PHP FTP Functions within the PHP Programming forums, part of the Web Development category; PHP FTP Introduction The FTP functions give client access to file servers through the File Transfer Protocol (FTP). The FTP ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| PHP FTP Introduction The FTP functions give client access to file servers through the File Transfer Protocol (FTP). The FTP functions are used to open, login and close connections, as well as upload, download, rename, delete, and get information on files from file servers. Not all of the FTP functions will work with every server or return the same results. The FTP functions became available with PHP 3. |
| Sponsored Links |
| |||
| Most of the PHP applications store and read their settings from a configuration file. Normally an installer script is used to write the settings to the config file and then an admin section is provided through which the administrator can manage these settings. Normal PHP file functions like fopen and fputs can be used to create and write to the configuration file. But there is a major drawback with this approach. If you want to create a file/directory through a PHP script in another directory, say, includes, then the directory includes need to be writable by the PHP script. Most of the times this means that you will need to chmod the directory to 777. This is a big security hole. Another problem is that, more often than not, the owner/group of the newly created file is set to that of the PHP process. PHP process normally run as “nobody” or “www”. This means that the newly created file will be owned by “nobody” or “www” and you might not be able to delete the file through FTP. You will have to delete the file through a PHP script. A simple workaround to the above problems would be to use the chmod and chown functions. Unfortunately both these functions don’t work on most shared hosting plans. Code: <?php
//Make the file writable by all
chmod('somefile',0777);
//Open the file and write to it
$fp = fopen('somefile', 'w');
fputs($fp,$data);
fclose($fp);
//Make the file writable only by the owner
chmod('somefile',0644);
?> Regards, R.Kamalakannan. |
| |||
| Connecting to FTP server Use ftp_connect function to connect to FTP server and use ftp_login function to login to FTP server. Code: <?php
//Connect to the FTP server
$ftpstream = @ftp_connect('localhost');
//Login to the FTP server
$login = @ftp_login($ftpstream, 'user', 'secret');
if($login) {
//We are now connected to FTP server.
}
//Close FTP connection
ftp_close($ftpstream);
?> R.Kamalakannan. Last edited by Kamalakannan : 12-17-2007 at 06:15 AM. |
| |||
| Creating directory through FTP Now that we have connected to FTP server, we can create a directory on the server using ftp_mkdir function. Code: <?php //Create a directory 'config' at the root of your website @ftp_mkdir($ftpstream, '/public_html/config'); ?> 1. Path specified is not the filesystem path 2. FTP root is different from web’s server document root You may specify the directory name only, that is, without the full path. In that case, the new directory will be created where ever you ended up when you logged into the FTP server. Regards, R.Kamalakannan. |
| |||
| Create file through FTP Creating a new file through FTP functions is a two step process. First we create a new temporary file using the tmpfile function. Next we upload this temporary file to the server using ftp_fput function. Code: <?php //Create a temporary file $temp = tmpfile(); //Upload the temporary file to server @ftp_fput($ftpstream, '/public_html/config/config.inc', $temp, FTP_ASCII); ?> R.Kamalakannan. |
| |||
| Change mode of file using ftp_site We can change the file mode/permissions using the ftp_site function. Code: <?php
//Make the file writable by all
ftp_site($ftpstream,"CHMOD 0777 /public_html/config/config.inc");
//Write to file
$fp = fopen('/home/jatinder/public_html/config/config.inc', 'w');
fputs($fp,'Learnt this at PHPSense.com');
fclose($fp);
//Make the file writable only to owner
ftp_site($ftpstream,"CHMOD 0644 /public_html/config/config.inc");
?> Regards, R.Kamalakannan. |
| |||
| File space allocates using ftp_alloc() The ftp_alloc() function allocates space for a file to be uploaded to the FTP server. This function returns TRUE on success or FALSE on failure. Code: <?php
$file = "myfile.txt";
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");
if (ftp_alloc($conn, filesize($file), $response))
echo "Space allocated on server.";
else
echo "Unable to allocate space. " . $response;
ftp_close($conn);
?> Regards, R.Kamalakannan. |
| |||
| ftp_chmod() The ftp_chmod() function sets permissions on a specified file on the FTP server. This function returns the new permissions on success and FALSE and a warning on failure. Code: <?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"user","pass");
// Read and write for owner, nothing for everybody else
ftp_chmod($conn,"0600","test.txt");
// Read and write for owner, read for everybody else
ftp_chmod($conn,"0644","test.txt");
// Everything for owner, read and execute for everybody else
ftp_chmod($conn,"0755","test.txt");
// Everything for owner, read for owner's group
ftp_chmod($conn,"0740","test.txt");
ftp_close($conn);
?> R.Kamalakannan. |
| |||
| ftp_raw() The ftp_raw() function sends a raw command to the FTP server. Note: This function returns the server response as an array of strings. No parsing is performed, and ftp_raw() does not check if the command was correct. Code: <?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
print_r (ftp_raw($conn,"USER admin"));
print_r (ftp_raw($conn,"PASS ert456"));
ftp_close($conn);
?> Code: Array ([0] => 331 User admin, password please) Array ([0] => 230 Password Ok, User logged in) R.Kamalakannan. |
| |||
| ftp_get_option() The ftp_get_option() function returns specified runtime behavior from the FTP connection. Syntax Code: ftp_get_option(ftp_connection,option)
Parameter Description
ftp_connection Required. Specifies the FTP connection to use
option Required. Specifies which runtime behavior to return.
Possible values:
* FTP_TIMEOUT_SEC - Returns the time limit for network operations
* FTP_AUTOSEEK - Returns TRUE if this option is on, FALSE otherwise Code: <?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");
echo ftp_get_option($conn,FTP_TIMEOUT_SEC);
ftp_close($conn);
?> R.Kamalakannan. |
| |||
| ftp_get() The ftp_get() function gets a file from the FTP server and saves it to a local file. This function returns TRUE on success and FALSE on failure. Code: ftp_get(ftp_connection,local,remote,mode,resume)
Parameter Description
ftp_connection Required. Specifies the FTP connection to use
local Required. Specifies where to save the file. If this file already exists, it will be overwritten
remote Required. Specifies the path of the file to copy from
mode Required. Specifies the transfer mode. The possible values are:
* FTP_ASCII
* FTP_BINARY
resume Optional. Specifies where in the remote file to start copying Code: <?php
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");
echo ftp_get($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);
?> R.Kamalakannan. |
| |||
| ftp_nb_fget() The ftp_nb_fget() function gets a file from the FTP server and saves it to an open file. This function returns one of the following values: * FTP_FAILED (send/receive failed) * FTP_FINISHED (send/receive completed) * FTP_MOREDATA (send/receive in progress) Unlike ftp_fget(), this function retrieves the file asynchronously. This means your program can perform other operations while the file is being downloaded. Code: ftp_nb_fget(ftp_connection,remote,local,mode,resume) remote - Required. Specifies the path of the file to copy from local - Required. Specifies the open file to paste into mode - Required. Specifies the transfer mode. The possible values are: * FTP_ASCII * FTP_BINARY resume - Optional. Specifies where in the remote file to start copying. Default is 0 Code: <?php
$source = "source.txt";
$target = fopen("target.txt", "w");
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");
ftp_nb_fget($conn,$source,$target,FTP_ASCII);
ftp_close($conn);
?> R.Kamalakannan. |
| |||
| ftp_nb_fput() The ftp_nb_fput() function uploads from an open file and saves it to a file on the FTP server. This function returns one of the following values: * FTP_FAILED (send/receive failed) * FTP_FINISHED (send/receive completed) * FTP_MOREDATA (send/receive in progress) Unlike ftp_fput(), this function retrieves the file asynchronously. This means your program can perform other operations while the file is being downloaded. Syntax ftp_nb_fput(ftp_connection,remote,local,mode,resum e) ftp_connection - Required. Specifies the FTP connection to use remote - Required. Specifies the file to upload to local - Required. Specifies the open file to upload from mode - Required. Specifies the transfer mode. The possible values are: * FTP_ASCII * FTP_BINARY resume Optional. Specifies where in the local file to start copying. Default is 0 Example This example copies the text from "source.txt" into "target.txt": PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| ftp_nb_get() The ftp_nb_get() function gets a file from the FTP server and saves it to a local file. This function returns one of the following values: * FTP_FAILED (send/receive failed) * FTP_FINISHED (send/receive completed) * FTP_MOREDATA (send/receive in progress) Unlike ftp_get(), this function retrieves the file asynchronously. This means your program can perform other operations while the file is being downloaded. Syntax ftp_nb_get(ftp_connection,local,remote,mode,resume ) ftp_connection - Required. Specifies the FTP connection to use local - Required. Specifies where to save the file. If this file already exists, it will be overwritten remote - Required. Specifies the path of the file to copy from mode - Required. Specifies the transfer mode. The possible values are: * FTP_ASCII * FTP_BINARY resume Optional. Specifies where in the remote file to start copying Example This example copies the text from "source.txt" into "target.txt": PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| ftp_nb_put() The ftp_nb_put() function uploads from a local file to the FTP server. This function returns one of the following values: * FTP_FAILED (send/receive failed) * FTP_FINISHED (send/receive completed) * FTP_MOREDATA (send/receive in progress) Unlike ftp_put(), this function retrieves the file asynchronously. This means your program can perform other operations while the file is being downloaded. Syntax ftp_nb_put(ftp_connection,remote,local,mode,resume ) ftp_connection - Required. Specifies the FTP connection to use remote - Required. Specifies the file to upload to local - Required. Specifies the path of the file to upload from mode - Required. Specifies the transfer mode. The possible values are: * FTP_ASCII * FTP_BINARY resume - Optional. Specifies where in the local file to start copying Example This example copies the text from "source.txt" into "target.txt": PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| ftp_fget() The ftp_fget() function gets a file from the FTP server and saves it to an open file. This function returns TRUE on success and FALSE on failure. Syntax ftp_fget(ftp_connection,local,remote,mode,resume) ftp_connection - Required. Specifies the FTP connection to use local - Required. Specifies the open file to paste into remote - Required. Specifies the path of the file to copy from mode - Required. Specifies the transfer mode. The possible values are: * FTP_ASCII * FTP_BINARY resume - Optional. Specifies where in the remote file to start copying. Default is 0 Example This example copies the text from "source.txt" into "target.txt": PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| ftp_cdup() The ftp_cdup() function changes the current directory to the parent directory (one step up). This function returns TRUE on success and FALSE on failure. Syntax ftp_cdup(ftp_connection) ftp_connection - Required. Specifies the FTP connection to use Example PHP Code: PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| ftp_chdir() The ftp_chdir() function changes the current directory to the specified directory. This function returns TRUE on success and FALSE and a warning on failure. Syntax ftp_chdir(ftp_connection,directory) ftp_connection - Required. Specifies the FTP connection to use directory - Required. Specifies the directory to change to Example PHP Code: PHP Code:
__________________ Thanks & Regards, R.Kamalakannan. |