0

I'm using a CentOS x86-64 Linux server, trying to run a python script from a php script called by an html-script. This is a well-covered topic on the internet, however nothing of the instructions and ideas work. I need this command chain later on to invoke an exe compiled on the server and to do some necessary pre-/post-processing of data. In order not to bore anybody with excess details of the actual computational task, I just present the most simple example here: uploading a local text file from an html-page and calling php to invoke a python script echoing "hello world". All scripts reside on the server: the html-script in my www-directory and the php- & python-scripts in a sub-directory shown below. I do not have sudo rights on the platform, so I need to manage with exec(), shell_exec() or system() without sudo, whichever may work in the end.

My questions:

(1) The chosen text file (mcase.fm.prm) is correctly identified but not moved to the specified subdirectory fileGMPuploads.

The php command echo $_FILES['userfile']['tmp_name'];

prints the file content correctly on the screen, hence the file has been correctly transferred from the local computer to the server. The php script manages to list the contents of the subdirectory fileGMPuploads with ls -al, but the text file mcase.fm.prm appears nowhere. What is wrong with my html- and php-scripts?

(2) Why cannot php execute the python file, signalling error code 127 (file not found), eventhough it knows the current directory and manages to list hello.py among the other files? Testing shows that exec() is enabled on the platform. Calling the test_function('exec') from the php-file returns

fn_enabled = 1 -> exec() exists/is enabled/safe mode not on

the function is added to the beginning of the php-file below.

I note from the php-output that the current directory is written as /data/wwwusers/gmp/clientV. I do not know from where php has invented the directory /data/?

From the command line on the linux server pwd returns /wwwusers/gmp/clientV as it should, whereas /data/wwwusers/gmp/clientV cannot be found. This does not distract the php-interpreter from listing the correct content of wwwusers/gmp/clientV, however. Does it disturb the php exec()/shell_exec() commands, and what to do about the problem?

When run from the command line, ./hello.py returns "hello world" as it should.

I have also tried both exec() and shell_exec() with the path hard wired, but without success. The error code 127 is persistent.

I present the short scripts and output below: (1) the html-script, (2) the php-script, (3) the python-script and (4) the output. The directories wwwusers/gmp and wwwusers/gmp/clientV have the xr rights for group/others just as the other components under clientV/.

I would very much appreciate any advice on how to proceed.

1. The html-script:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html;
      charset=windows-1252">
    <title> calling checkFileUpload.php </title>
  </head>
  <body style="text-align:center;">
    <form enctype="multipart/form-data"
      action="clientV/checkFileUpload.php"
      method="POST">
        name="MAX_FILE_SIZE" value="30000" type="hidden">
      Send this file: <input name="userfile" type="file"> <input value="Send File" type="submit">
    </form>
  </body>
</html>

2. the php-file checkFileUpload.php:

<?php

function test_function($test_fn) {
// Exec function exists.
// Exec is not disabled.
// Safe Mode is not on.
$fn_enabled =
function_exists($test_fn) &&
!in_array($test_fn, array_map('trim', explode(', ',
ini_get('disable_functions')))) &&
strtolower(ini_get('safe_mode')) != 1;

if($fn_enabled) { echo "fn_enabled = " . $fn_enabled . " -> " . $test_fn . "() exists/is enabled/safe mode not on\n"; }
else { echo "fn_enabled = " . $fn_enabled . " -> " . $test_fn . "() is not enabled\n"; }
} /* end of test_function() */

echo '<pre>';

$uploaddir = 'firmGMPuploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} 
else {
  echo "upload unsuccessful\n";
}

print_r($_FILES);

echo "local path and file identification variables:\n";
echo $uploaddir;
echo "\n";
echo $uploadfile;
echo "\n";

echo "current directory: " . getcwd() . "<br>";

exec('pwd', $outputArray);
exec('ls -al', $outputArray);
print_r($outputArray);

echo "the contents of the sub-directory clientV/firmGMPuploads/:\n";
unset($outputArray);
exec("ls -al '".$uploaddir."'",$outputArray);
print_r($outputArray);

unset($outputArray);                              /* try shell_exec() */
$outputArray = shell_exec('./hello.py');
echo $outputArray;
echo "\n";

$command = './hello.py';                          /* try exec() */
unset($outputArray);
$result_code=null;
exec($command, $outputArray, $result_code);

echo $command;
echo "\n";
print_r($outputArray);
echo " result_code: ";
echo $result_code;
echo "\n";
print "</pre>";

?>

3. the python file hello.py:

#!/usr/bin/env python3

print('hello world')

4. output generated by the php-file after choosing a local text-file (mcase.fm.prm) in the web browser and pressing the "Send File" button:

upload unsuccessful

Array
(
    [userfile] => Array
        (
            [name] => mcase.fm.prm
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpWRRYNf
            [error] => 0
            [size] => 9646
        )  
)  

local path and file identification variables:
firmGMPuploads/
firmGMPuploads/mcase.fm.prm
current directory: /data/wwwusers/gmp/clientV

Array
(
    [0] => /data/wwwusers/gmp/clientV
    [1] => total 17
    [2] => drwxr-xr-x 4 gmp    7 Feb 11 20:05 .
    [3] => drwxr-xr-x 9 gmp   30 Feb 11 19:35 ..
    [5] => -rwxr-xr-x 1 gmp 1418 Feb 11 20:05 checkFileUpload.php
    [7] => drwxr-xr-x 2 gmp    3 Feb 11 11:09 firmGMPuploads
    [8] => -rwxr-xr-x 1 gmp   45 Feb 11 09:13 hello.py
)

the contents of the sub-directory clientV/firmGMPuploads/:

Array
(
    [0] => total 6
    [1] => drwxr-xr-x 2 gmp  3 Feb 11 11:09 .
    [2] => drwxr-xr-x 4 gmp  7 Feb 11 20:05 ..
    [3] => -rwx------ 1 gmp 96 Feb 11 11:09 del.csh
)

./hello.py

Array
(
)

result_code: 127

***** an additional small test *****

Testing php with a text file succeeds without problems and sudo rights from html - I do not even need to point to the current directory. The php script and the text file cwtb.txt reside in the directory /wwwusers/gmp/:

<?php
$hh = file_get_contents('cwtb.txt');
$hh=$hh+1;
file_put_contents('cwtb.txt', $hh);

header('Location:pdf_cwtb/cwtb.pdf'); // redirect to the real file to be downloaded
?>

the file is updated and redirection works as it should.

***** end of additional test ******

Anthony
  • 1
  • 1
  • Why are you using an obsolete Linux distribution? How has it been customized? – Michael Hampton Feb 11 '21 at 22:05
  • I'm waiting for an update at our university. The reported problem is of basic nature and should also work with old distributions. Maybe customization is the problem. – Anthony Feb 12 '21 at 08:10

0 Answers0