ssh2_scp_send not accepting array variable more than 9 - php

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

Related

PHP IMAP connection to Exchange works but throws errors

Searched all through Server Fault and Stack Overflow, but cannot find any useful information regarding this issue I have. There are plenty of questions regarding getting connected to Exchange, but that's not where my issue is.
I am running php-imap (latest via composer) and have setup a PHP script to connect to Office 365/Exchange to check for bounced messages. The script executes and works correctly, however throws some confusing connection errors.
The basic script:
$imap_server = 'outlook.office365.com';
$imap_folder = 'INBOX';
$imap_user = '((myemailaddress))';
$imap_pass = '((mypasswrod))';
$mailbox = new PhpImap\Mailbox ('{' . $imap_server . '}' . $imap_folder, $imap_user, $imap_pass);
try {
$mailsIds = $mailbox->searchMailbox('ALL', true);
print_r ($mailsIds);
} catch(PhpImap\Exceptions\ConnectionException $ex) {
echo "IMAP connection failed: " . $ex;
die();
}
Output:
Array
(
[0] => 47
[1] => 49
[2] => 51
[3] => 53
[4] => 55
[5] => 57
[6] => 59
)
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
Notice: Unknown: Connection failed to MNZ-efz.ms-acdc.office.com,143: No route to host (errflg=1) in Unknown on line 0
EDIT:
Further - it appears all emails in the INBOX are being deleted upon execution of the script. Not sure why... but it seems it would have something to do with whatever is happening here in the background that is causing those errors.

imap_open(): Couldn't open stream on linux server

My code is working fine when run on windows laptop with XAMPP, but when I try to run same code on linux machine it is giving error.
My code:
<?php
$connect_to = '{imap.gmail.com:993/imap/ssl}INBOX';
$user = 'my gmail address';
$password = 'my gmail password';
$connection = imap_open($connect_to, $user, $password)
or die("Can't connect to '$connect_to': " . imap_last_error());
imap_close($connection);
?>
Error I am getting:
PHP Warning:imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/re-work/test_imap.php on line 30
Array
(
[type] => 2
[message] => imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX
[file] => /home/re-work/test_imap.php
[line] => 30
)
PHP Warning: imap_num_msg() expects parameter 1 to be resource, boolean given in /home/re-work/test_imap.php on line 39
PHP Warning: imap_search() expects parameter 1 to be resource, boolean given in /home/re-work/test_imap.php on line 42
0</br>PHP Notice: Unknown: Retrying PLAIN authentication after [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=1) in Unknown on line 0
PHP Notice: Unknown: Retrying PLAIN authentication after [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=1) in Unknown on line 0
PHP Notice: Unknown: Can not authenticate to IMAP server: [ALERT] Please log in via your web browser: https://support.google.com/mail/acco (errflg=2) in Unknown on line 0
Please help where and what I am missing?

PHP file upload: No such file or directory

I've a big problem I'm working on for days now. My file upload in PHP does not work for the following reason.
Here are some parts of my code:
# Upload file1
move_uploaded_file($_FILES['ev_ap_file1']['tmp_name'], 'as-content/'.$_FILES['ev_ap_file1']['name']);
# Upload file2
move_uploaded_file($_FILES['ev_ap_file2']['tmp_name'], 'as-includes/css/'.$_FILES['ev_ap_file2']['name']);
Infos about the files
Array
(
[ev_ap_file1] => Array
(
[name] => test.php
[type] => application/octet-stream
[tmp_name] => /tmp/phpfQIobp
[error] => 0
[size] => 0
)
[ev_ap_file2] => Array
(
[name] => test.css
[type] => text/css
[tmp_name] => /tmp/phpnQqf5T
[error] => 0
[size] => 0
)
)
And finally the error message I get:
Warning: move_uploaded_file(as-content/test.php): failed to open stream: No such file or directory in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 47
Warning: move_uploaded_file(): Unable to move '/tmp/phpfQIobp' to 'as-content/test.php' in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 47
Warning: move_uploaded_file(as-includes/css/test.css): failed to open stream: No such file or directory in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 49
Warning: move_uploaded_file(): Unable to move '/tmp/phpnQqf5T' to 'as-includes/css/test.css' in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 49
Dev Exit enabled.
I already tried a lot of solutions and nothing worked. I'm the owner of the directory and of course it exist.
Thank you for your help!
Greetings, Arne
Possible Cases
Make sure have folder exist or you have enough permission to create new folder you are trying to move your file.
Folder [You want to move] must have write permission.
Apache user must have access of /tmp or temporary directory (set in php.ini "tmp_path").
Make sure server is not running out of memory`
Make sure file is not bigger than max_upload_size parameter

Upload file from local to SFTP via PHP SSH2

I am trying to upload a local file from my Mac to SFTP via PHP. My code:
$connection = ssh2_connect($server, $port);
if (ssh2_auth_password($connection, $username, $passwd)) {
$sftp = ssh2_sftp($connection);
echo "Connection successful, uploading file now..."."\n";
$file = '/Users/petenaylor/Desktop/diamondexclusive.mp4';
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/{$file}", $contents);
}
else {
echo "Unable to authenticate with server"."n";
}
It connects as it should and I have checked that the local file location is correct, but the error messages I get are:
Warning: file_get_contents(/Users/petenaylor/Desktop/diamondexclusive.mp4): failed to open stream: No such file or directory in /home/test.php on line 39
Warning: file_put_contents(): Unable to open ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4 on remote host in /home/test.php on line 40
Warning: file_put_contents(ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4): failed to open stream: operation failed in /home/test.php on line 40
My log file from Filezilla:
Command: put "/Users/petenaylor/Desktop/diamondexclusive.mp4" "diamondexclusive.mp4"
Status: local:/Users/petenaylor/Desktop/diamondexclusive.mp4 => remote:/home/myfarewellnote/web/diamondexclusive.mp4
Trace: FileTransferParseResponse(0)
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: File transfer successful, transferred 12,661,295 bytes in 111 seconds
Status: Retrieving directory listing of "/home/myfarewellnote/web"...
Trace: CSftpControlSocket::ParseSubcommandResult(0)
Trace: CSftpControlSocket::ListSubcommandResult()
Trace: CSftpControlSocket::SendNextCommand()
Trace: CSftpControlSocket::ListSend()
Command: ls
Status: Listing directory /home/myfarewellnote/web
Trace: CSftpControlSocket::ListParseResponse()
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/home/myfarewellnote/web" successful
In FileZilla, you upload a file under a name diamondexclusive.mp4 to the current remote working directory, which is /home/myfarewellnote/web.
Hence the full target path is /home/myfarewellnote/web/diamondexclusive.mp4.
While in PHP you upload the file to /Users/petenaylor/Desktop/diamondexclusive.mp4 (what is actually a local source path, that has nothing to do with the server).
Use the same path that you upload the file to in FileZilla:
file_put_contents("ssh2.sftp://{$sftp}/home/myfarewellnote/web/diamondexclusive.mp4", $contents);

Writing a File to a local share from a webserver

I am trying to write a file to a local share using PHP and a webserver. The origin of the file is the webserver ubuntu01 and a share on my local machine 7PD01-2012 is where the file is to be written. The path "ftp://ryan:pass#7PD01-2012//file.txt" resolves when I put it in a web browser and displays the file contents. There does not seem to be a permissions issue. I am able to ping the webserver from the client and the client from the webserver. FTP is installed on my ubuntu webserver and Filezille ftp Server is installed on the client. Using filezilla or the browser I am able to ftp to both locations. I have setup firewall rules on the webserver and updated the /etc/hosts file. Any ideas what could be causing these errors?
$fileName = "ftp://ryan:pass#7PD01-2012//file.txt";
is_dir($fileName);
if (($myfile = fopen($fileName, "w")) === false) { //open the file
//if unable to open throw exception
throw new RuntimeException("Could Not Open File Location on Server.");
}
( ! ) Warning: is_dir(): connect() failed: Connection timed out in /var/www/html/CDprinter/processor.php on line 143
( ! ) Warning: fopen(): connect() failed: Connection timed out in /var/www/html/CDprinter/processor.php on line 144
( ! ) Warning: fopen(ftp://...#10.162.12.130//file.txt): failed to open stream: operation failed in /var/www/html/CDprinter/processor.php on line 144

Categories