1

I made an API in PHP which receives a input.csv file in the form of a post request and that file is saved in the root user of a digital ocean server. With the help of this file I used exec command in PHP to run a python script kept in one of the folder's of the root user in the same digital ocean server. After running the python script it will create another csv file named as output.csv which is then read by the API and is given as a response to the hit request at the digital ocean server.

But the problem that I am facing is whenever I am trying to hit the URL of the digital ocean server using postman or browser as a post request for testing I do not see the output.csv file getting created in the root user group of the server. However I could see the input.csv file being created which is passed as a parameter while hitting the url.The reason which I could sense from my analysis is that the php api which I have created is not able to run the python script which produces the output.csv file whose content I am interested in.

One more thing as I am hitting the url from apache server so it is from www-data user group and the server where the python file lies is from root user group. I just wrote this to know if there is any permission related issue or not.

I am using exec command to execute the python script. The code of my api is given below any help would be appreciated.

<?php
        $input_file_content = base64_decode($_POST['file_contents']);
        print_r($input_file_content);
        $python_file = "/var/www/html/selenium/scrap.py";
        $input_file = dirname(__FILE__) . "/input.csv";
        $output_file ="/var/www/html/selenium/output2.csv";
        file_put_contents($input_file, $input_file_content);
        exec("whoami 2>&1", $output, $return_var);
        print_r($output);
        exec("sudo /root/anaconda2/envs/venv/bin/python " . $python_file);
                    echo "<final_string>";
        echo base64_encode(file_get_contents($output_file));
        echo "</final_string>";
     ?>
Oliver
  • 11,857
  • 2
  • 36
  • 42
Sumit Kapoor
  • 1,089
  • 12
  • 10

0 Answers0