简体   繁体   中英

ssh2_scp_send not accepting array variable more than 9

this is my php script, here iam trying to upload photos to other server using SSH in that when i scan directory and store it in an array.

then i try to run in terminal upto index 9 its uploading properly, issue come when index comes at 10...........................................................................................................................................

$username = 'root';
$password = 'Workflow#5992$';

$connection = ssh2_connect($host, 22);


ssh2_auth_password($connection, $username, $password);

$sftp = ssh2_sftp($connection);



// Specifying directory
$mydir = '/var/www/html/AcadWFReport/photos';

// Scanning files in a given directory in ascending order
$myfiles = scandir($mydir);

// Displaying the files in the directory
print_r($myfiles);


$c = count($myfiles);
//print_r($c);

for ($i = 2; $i < $c; $i++){    
$fname= substr($myfiles[$i],0,8); 
$ext= substr($myfiles[$i],9,3);
$b= substr($myfiles[$i],2,2);
$batch=20 . $b ;
$pid= substr($myfiles[$i],4,1);
$pname=$myfiles[$i];

$result1=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'');
$result2=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'');

$local_file = '/var/www/html/AcadWFReport/photos/'.$pname.'';
$remote_file = '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'';






echo 'Uploading...'.$pname.''.$i.''."\n";

$stream = file_exists("ssh2.sftp://$connection/var/www/html/Dev/File_upload/doc/byroll/".$batch."/".$pid."/".$pname."");
if($stream){
    
    echo "File already Exits";
    $new_name= $fname.'_'.date('dmY').'.'.$ext;

    ssh2_sftp_rename($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'', '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$new_name.'');
}

$result = ssh2_scp_send($connection, $local_file, $remote_file, 0644);

if(!$result) {
    echo 'Error while uploading';
    
}
else{

echo 'Uploaded successfully!'."\n";

}
}

and my output is below

[root@localhost AcadWFReport]# php transfer.php
Array
(
    [0] => .
    [1] => ..
    [2] => ME18S010.JPG
    [3] => MM17S001.JPG
    [4] => MS12B043.JPG
    [5] => MS15F001.JPG
    [6] => MS16D003.JPG
    [7] => MS19S034.JPG
    [8] => MS20B036.JPG
    [9] => MS21D003.JPG
    [10] => MS21W002.JPG
    [11] => MS21W004.JPG
    [12] => MS21W005.JPG
    [13] => MS21W007.JPG
    [14] => MS21W008.JPG
    [15] => MS21W009.JPG
    [16] => MS21W010.JPG
    [17] => MS21W011.JPG
    [18] => MS21W012.JPG
    [19] => MS21W014.JPG
)
Uploading...ME18S010.JPG2
File already ExitsUploaded successfully!
Uploading...MM17S001.JPG3
File already ExitsUploaded successfully!
Uploading...MS12B043.JPG4
File already ExitsUploaded successfully!
Uploading...MS15F001.JPG5
File already ExitsUploaded successfully!
Uploading...MS16D003.JPG6
File already ExitsUploaded successfully!
Uploading...MS19S034.JPG7
File already ExitsUploaded successfully!
Uploading...MS20B036.JPG8
File already ExitsUploaded successfully!
Uploading...MS21D003.JPG9
File already ExitsUploaded successfully!
Uploading...MS21W002.JPG10
File already ExitsPHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W004.JPG11
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W005.JPG12
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W007.JPG13
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W008.JPG14
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W009.JPG15
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W010.JPG16
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W011.JPG17
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W012.JPG18
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W014.JPG19
PHP Warning:  ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60 ```

and after index 9 it not allowing any double digit number to it 

You start SFTP session (ssh2_sftp), while you use SCP later for actual transfer (ssh2_scp_send). You definitely do not need the ssh2_sftp line; and it can actually be a cause of your problem. While technically it is possible to have both SFTP and SCP sessions over one SSH connection, I would not expect PHP to support this. Though I'm not sure.

and I commented the SFTP part in my code and uploaded more than 50 file though ssh2

And its working perfectly fine all 57 files have been moved to remote server from local server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM