0

So I wrote a bash script that takes in 2 parameters.

  1. Local filename
  2. File name to be stored in the object store.

I want a php script to be able to run this and so I wrote something like this:

<?php

    $output = shell_exec("./uploadFile.sh 'confirmation.txt' 'test_conf_script'");
    echo $output;
?>

This is obviously hardcoded but worked fine using the command:

php -f test.php.

Now I wanted my web application to be able to upload a file to the object store using the same bash script and so I modified my php script like this:

<?php

    $target_dir = "./upload/";
    $name = (string)$_POST['name'];
    $file_name = (string)basename($_FILES["file"]["name"]);
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $file_type = $_FILES["file"]["type"];
    $file_size = $_FILES["file"]["size"];

    if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)){
            echo "Upload\n";
    }
    else{
            echo "couldn't upload!";
    }


    $output = shell_exec("./uploadFile.sh '$file_name' '$name'");

    echo "./uploadFile.sh '$file_name' '$name'";
?>

My frontend provides the script with the file and I do see my file locally.

However the uploading to the object store using shell_exec doesnt work..

It seems bizzare since its exactly the same as the test.php file and although that works from the terminal, when my frontend triggers this php script it doesnt work.

Vadim Beskrovnov
  • 941
  • 6
  • 18
  • Check the permissions on `uploadFile.sh` (owner:group), that's likely where the issue is... – l'L'l Jul 18 '17 at 04:58
  • permissions for uploadFile.sh -rwxr-xr-x – Aditya Bhargava Jul 18 '17 at 05:16
  • 1
    Check your current working dir with `echo getcwd();`. Are you in the same folder as `uploadFile.sh`? – hek2mgl Jul 18 '17 at 08:05
  • Yes. My working directory is /var/www/html/ajax which is the same as where uploadFile.sh is. I dont think its an issue with directory since my test.php file worked just fine. It included the same code to execute shell just hardcoded the filename and name variables. I made sure these variables were correct using the echo statement.. – Aditya Bhargava Jul 18 '17 at 13:45
  • Can you be more specific about "uploading to the object store using shell_exec doesn't work"? Are you receiving an error? What's the value of `$output`? Have you tried logging when your shell script runs to see what options and attributes it's receiving? – Aken Roberts Jul 19 '17 at 01:37

1 Answers1

0

Have you ever tried this :

You probably need to chdir to the correct directory before calling the script. This way you can ensure what directory your script is "in" before calling the shell command

$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh var1 var2');
chdir($old_path);
4givN
  • 2,936
  • 2
  • 22
  • 51
  • My working directory is /var/www/html/ajax which is the same as where uploadFile.sh is. I dont think its an issue with directory since my test.php file worked just fine. It included the same code to execute shell just hardcoded the filename and name variables. I made sure these variables were correct using the echo statement.. – Aditya Bhargava Jul 18 '17 at 13:45