Before Starting:
Know About File Location
- This is my server side location where my index.php stand :
/public_html/filetransfer/index.php
- This is my server side text file which i want to transfer :
/public_html/filetransfer/transfer.txt
- This is my another server side location where i want to transfer :
Follow the Step:
Step 1: Suppose your index.php file is already in a server.
Step2: When you run the index.php you will have to enter
Another servername(hostname)like(abc.com),username,password.
Step 3: Enter Source path (which file you want to transfer) and Destination Path(where you want to store).
Note: If your index.php file is exist in your current folder
like filetransfer/index.php then we just need to enter
filename on source path.
Ex:
filetransfer->index.php,transfer.text,abc.txt...etc
Source path='transfer.txt'
/*we do not need to write full path*/ .we just have
to enter filename.
If transfer file is in : /public_html/filetransfer/find/my/file/transfer.txt
Source path ='find/my/file/transfer.txt ;
Destiantion path=/public_html/destination /*where we want to store that file */
index.php
<?php
$ftp_server=$_POST['servername']; <?php
$ftp_username=$_POST['username'];
$ftp_userpass=$_POST['password'];
$conn_id = ftp_connect($ftp_server); $login_result =ftp_login($conn_id,$ftp_username,$ftp_userpass);
if($login_result) {
ftp_pasv($conn_id, true);$source_path='transfer.txt';
$remote_dir=/public_html/destination;
$remote_file='transfer_filename.txt';
if(!@ftp_chdir($conn_id, $remote_dir))
{
ftp_mkdir($conn_id, $remote_dir);
ftp_chdir($conn_id, $remote_dir);
$upload =ftp_put($conn_id,$remote_file,$source_path,FTP_BINARY);
print (!$upload) ? 'Cannot upload' : 'Upload complete';
}else
{
echo"Connection Failed";
}
?>
Terminology
ftp_connect() : opens an FTP connection to the specified
Host
.ftp_pasv(): turns on or off passive mode. In passive mode, data connections are initiated by the client, rather than by the server. It may be needed if the client is behind firewall.
No comments:
Post a Comment