IT Community - Software Programming, Web Development and Technical Support

PHP FTP Functions

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-17-2007, 05:52 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default PHP FTP Functions

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-17-2007, 06:02 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Therefore we will use PHP FTP functions to create files, directories and to change the mode of files. The functions that are of importance to use here are: ftp_mkdir, ftp_put and ftp_site.

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-17-2007, 06:06 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Regards,
R.Kamalakannan.

Last edited by Kamalakannan : 12-17-2007 at 06:15 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-17-2007, 06:11 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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');
?>
Notice that path specified here is not the filesystem path, its the FTP path. /public_html here is the FTP root and is often different from web server’s document root. Couple of points to keep in mind:

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-17-2007, 06:18 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-17-2007, 06:23 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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");
?>
PHP5 has built in ftp_chmod, so if you are running PHP5 you can use the ftp_chmod function instead of the above PHP code snippet.

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-18-2007, 07:04 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Note: Many FTP servers do not support this command.

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-18-2007, 07:12 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-18-2007, 07:19 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Output:
Code:
Array ([0] => 331 User admin, password please) 
Array ([0] => 230 Password Ok, User logged in)
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-28-2007, 06:29 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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);
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 12-28-2007, 06:51 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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
This example copies the text from "source.txt" into "target.txt":
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);
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 01-05-2008, 02:08 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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)
ftp_connection - Required. Specifies the FTP connection to use
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);
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 03-31-2008, 03:30 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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:
<?php
$source 
fopen("source.txt","r");

$conn ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

ftp_nb_fput($conn,"target.txt",$source,FTP_ASCII);

ftp_close($conn);
?>
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 03-31-2008, 04:05 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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:
<?php
$conn 
ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

ftp_nb_get($conn,"target.txt","source.txt",FTP_ASCII);

ftp_close($conn);
?>
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 03-31-2008, 04:10 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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:
<?php
$conn 
ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

ftp_nb_put($conn,"target.txt","source.txt",FTP_ASCII);

ftp_close($conn);
?>
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 03-31-2008, 04:13 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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:
<?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_fget($conn,$target,$source,FTP_ASCII);

ftp_close($conn);
?>
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 03-31-2008, 04:33 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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
$conn 
ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

//Outputs the current directory
echo "Dir: ".ftp_pwd($conn);
echo 
"<br />";
//Change to the images directory
ftp_chdir($conn,"images");
echo 
"Dir: ".ftp_pwd($conn);
echo 
"<br />";
//Change current directory to parent directory
ftp_cdup($conn);
echo 
"Dir: ".ftp_pwd($conn);

ftp_close($ftp_server);
?>
Output:
PHP Code:
Dir: /
Dir: /images
Dir
: / 
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 03-31-2008, 04:37 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: PHP FTP Functions

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
$conn 
ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"admin","ert456");

//Outputs the current directory
echo "Dir: ".ftp_pwd($conn);
echo 
"<br />";
//Change to the images directory
ftp_chdir($conn,"images");
echo 
"Dir: ".ftp_pwd($conn);

ftp_close($ftp_server);
?>
Output:
PHP Code:
Dir: /
Dir: /images 
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 03-31-2008, 07:36 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: PHP FTP Functions

Quote:
Originally Posted by Kamalakannan View Post
ftp_fget()

The ftp_fget() function gets a file from the FTP server and saves it to an open file.

This function