1

I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.

I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.

The script and the PHP file are in the same folder.

This is the PHP code:

<?php 
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result; 
?>

And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.

#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF" 
sftp UserXX@nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
mnille
  • 1,328
  • 4
  • 16
  • 20
  • try http://stackoverflow.com/questions/10894554/redirect-output-of-command-with-heredoc/10894659#10894659 – JYoThI May 11 '16 at 09:00
  • try http://stackoverflow.com/questions/32412839/including-some-sftp-commands-in-a-makefile – JYoThI May 11 '16 at 09:00

2 Answers2

0

exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.

Community
  • 1
  • 1
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
0

you have to give the full path to file

and use this 2>&1 and know the error

try something like this

$comando = "sh pathTofile/location/ftpgesdoc.sh";

if(exec("$comando  2>&1", $output, $return_var))
{
    print_r($output);
    echo "<br>";
   print_r($return_var);

}
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • Thanks, I tried with the full path, but I have the same result- nothing happens after the echo. – Jordi Fernández May 11 '16 at 08:56
  • I tried again, and I have more information about the error. It seems that I need some additional library. Thanks again. => /usr/bin/ssh: /opt/lampp/lib/libcrypto.so.1.0.0: version `OPENSSL_1.0.1' not found (required by /usr/bin/ssh) [2] => Couldn't read packet: Connection reset by peer ) – Jordi Fernández May 12 '16 at 07:46