Tuesday, 11 June 2013

SSH Configure in PHP

If i want to upload files in multi server then we will be use of SSH using of that i can upload a file in
simultaneously in  servers:-

SSH INSTILLATION link given below:-

http://php.net/manual/en/ssh2.installation.php

ex code here :-

create object :-
 $multiServerObj->UpdateUploadFileSsh($chann_icon_name,$_FILES["chan_icon"]["tmp_name"],'notuse',$deleteIcon);

Than

according to need use given  function:-



<?php
class SshUplaodImage{
function UploadFileSsh($chan_icon_name,$tempFileValue,$previewImg=null){
$host=$ip;
$user = 'root';
$pass = 'Matrix@123';
if($previewImg=='previewImg'){
$destDir = '/var/www/html/unified/admin/upload';
}else{
$destDir = '/var/www/html/unified/admin/upload/icon';

}
$HostServersCount = 4;

//$workDir = '../../tmp'; // define this as per local system
$workDir = '/var/www/html/unified/admin/includes/tmp'; // define this as per local system

$chan_icon_name;

// get temporary file name for the uploaded file
 $tmpName = basename($tempFileValue);


// copy uploaded file into current directory
if($TotalHost==4){
move_uploaded_file($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}else{
copy($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}

// open connection
for($i=0;$i<$HostServersCount;$i++){
if($i==0){
$host="211.01.02.50";
}else if($i==1){
$host="211.01.02.51";
}else if($i==2){
$host="211.01.02.54";
}else if($i==3){
$host="211.01.02.55";
}
$conn = ssh2_connect($host,22) or die ("Cannot initiate connection to host");

// send access parameters
ssh2_auth_password($conn, $user, $pass) or die("Cannot login");

// perform file upload
$upload = ssh2_scp_send($conn, $workDir.'/'.$tmpName, $destDir.'/'.$chan_icon_name, 0777);

// check upload status
// display message
if (!$upload) {
 echo "Cannot upload";
} else {
 echo "Upload complete";
}
ssh2_exec($conn, 'ls -al; echo "__COMMAND_FINISHED__"' );
//ssh2_exec($conn, 'exit');
}
// close the FTP stream
//ftp_close($conn);

// delete local copy of uploaded file
//die;
unlink($workDir.'/'.$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");

return $chann_icon_name ;
}

function UpdateUploadFileSsh($chan_icon_name,$tempFileValue,$previewImg=null,$deleteIcon=null){

$host=$ip;
$user = 'root';
$pass = 'Matrix@123';
if($previewImg=='previewImg'){
$destDir = '/var/www/html/unified/admin/upload';
}else{
$destDir = '/var/www/html/unified/admin/upload/icon';

}
$HostServersCount = 4;

//$workDir = '../../tmp'; // define this as per local system
$workDir = '/var/www/html/unified/admin/includes/tmp'; // define this as per local system

//echo $chan_icon_name;

// get temporary file name for the uploaded file
 $tmpName = basename($tempFileValue);


// copy uploaded file into current directory
if($TotalHost==4){
move_uploaded_file($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}else{
copy($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}

// open connection
for($i=0;$i<$HostServersCount;$i++){
if($i==0){
$host="211.01.02.50";
}else if($i==1){
$host="211.01.02.51";
}else if($i==2){
$host="211.01.02.54";
}else if($i==3){
$host="211.01.02.55";
}
$conn = ssh2_connect($host,22) or die ("Cannot initiate connection to host");

// send access parameters
ssh2_auth_password($conn, $user, $pass) or die("Cannot login");

if($deleteIcon!=null){
//unlink($destDir.'/'.$deleteIcon);

$sftp = ssh2_sftp($conn);
ssh2_sftp_unlink($sftp, $destDir.'/'.$deleteIcon);
}
if($previewImg!=null){
$sftp = ssh2_sftp($conn);
ssh2_sftp_unlink($sftp, $destDir.'/'.$deleteIcon);
}

// perform file upload
$upload = ssh2_scp_send($conn, $workDir.'/'.$tmpName, $destDir.'/'.$chan_icon_name, 0777);

// check upload status
// display message
if (!$upload) {
  "Cannot upload";
} else {
  "Upload complete";
}
ssh2_exec($conn, 'ls -al; echo "__COMMAND_FINISHED__"' );
//ssh2_exec($conn, 'exit');
}
// close the FTP stream
//ftp_close($conn);

// delete local copy of uploaded file
//die;
unlink($workDir.'/'.$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");

return $chann_icon_name ;
}
}

class uploadBuild{


function multiUploadAndroidBuild($chan_icon_name,$tempFileValue,$deleteIcon=null){

//$host=$ip;
$user ='root';
$pass = 'Matrix@123';
// $pass ='Cyberlinks';


$workDir = '/var/www/html/unified/admin/includes/tmp'; // define this as per local system
$destDir = '/var/www/html/unified/admin/upload/builds';
// get temporary file name for the uploaded file
$tmpName = basename($tempFileValue);


if($TotalHost==4){
move_uploaded_file($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}else{
copy($tempFileValue, $workDir.'/'.$tmpName) or die("Cannot move uploaded file to working directory");
}


$HostServersCount = 4;
// open connection
for($i=0;$i<$HostServersCount;$i++){
if($i==0){
$host="211.01.02.50";
//$host="211.01.02.45";
}else if($i==1){
$host="211.01.02.51";
}else if($i==2){
$host="211.01.02.54";
}else if($i==3){
$host="211.01.02.55";
}
$conn = ssh2_connect($host,22) or die ("Cannot initiate connection to host");

// send access parameters
ssh2_auth_password($conn, $user, $pass) or die("Cannot login");
//ssh2_auth_password($conn, 'root', 'Cyberlinks') or die("Cannot login");

if($deleteIcon!=null){
//unlink($destDir.'/'.$deleteIcon);

$sftp = ssh2_sftp($conn);
ssh2_sftp_unlink($sftp, $destDir.'/'.$deleteIcon);
}



// perform file upload
$upload = ssh2_scp_send($conn, $workDir.'/'.$tmpName, $destDir.'/'.$chan_icon_name, 0777);

// check upload status
// display message
if (!$upload) {
  "Cannot upload";
} else {
  "Upload complete";
}
ssh2_exec($conn, 'ls -al; echo "__COMMAND_FINISHED__"' );
//ssh2_exec($conn, 'exit');
}
// close the FTP stream
//ftp_close($conn);

// delete local copy of uploaded file
//die;
unlink($workDir.'/'.$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");


//return $chann_icon_name ;



}

}

?>

No comments:

Post a Comment