Uploading files using FTP in PHP
Installation
In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or greater or --with-ftp when using PHP 3.
The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.
Runtime Configuration
This extension has no configuration directives defined in php.ini .
Steps to be followed to upload files using FTP
1)Connect to your FTP server using the funtion ftp_connect()
2)Login to server using Username/password with the function ftp_login()
3)Transfer the files you want using the function ftp_put()
<?php
$conn_id="ftp.yourserver.com";//Write in the format "ftp.servername.com"
// make a connection to the ftp server
$conn_id = ftp_connect ( $ftp_server );
$ftp_user_name="username";
$ftp_user_pass="password";
// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
echo "Attempted to connect to $ftp_server for user $ftp_user_name" ;
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name" ;
}
$destination_file="index.html";
$source_file="index.html";
// upload the file
$upload = ftp_put ( $conn_id , $destination_file , $source_file , FTP_BINARY );
// check upload status
if (! $upload ) {
echo "FTP upload has failed!" ;
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file" ;
}
// close the FTP stream
ftp_close ( $conn_id );
?>
Script to upload a file via FTP
Create a file named "upload.html"
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%">server name </td>
<td width="75%"><input name="server" type="text" id="server">Write in the format "ftp.servername.com" </td>
<td width="11%"> </td>
</tr>
<tr>
<td>user name </td>
<td><input name="username" type="text" id="username"></td>
<td> </td>
</tr>
<tr>
<td>password</td>
<td><input name="password" type="password" id="password"></td>
<td> </td>
</tr>
<tr>
<td>File name </td>
<td><input type="file" name="file"></td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Upload"></td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
Now create a file named "upload.php"
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$ftp_server=$_POST['server'];
$ftp_user_name=$_POST['username'];
$ftp_user_pass=$_POST['password'];
$source_file=$_FILES['file']['name'];// retrieve name of the file to be uploaded
$destination_file=$source_file;
// make a connection to the ftp server
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id , $ftp_user_name , $ftp_user_pass);
// check connection
if((!$conn_id)||(!$login_result)){
echo "FTP connection has failed!" ;
echo "Attempted to connect to $ftp_server for user $ftp_user_name" ;
exit;
}else{
echo "Connected to $ftp_server, for user $ftp_user_name" ;
}
// upload the file
$upload = ftp_put($conn_id,$destination_file,$source_file,FTP_ASCII );
// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
}else{
echo "Uploaded $source_file to $ftp_server as $destination_file" ;
}
// close the FTP stream
ftp_close($conn_id);
?>
</body>
</html>
Back to Tutorial Index page