How to properly run a Python script from PHP
<p>I have a PHP script that unzips an archive containing Python scripts into a directory, str_replaces some things inside the script, shell_exec's the script, and then unlinks the script.</p>

<p>I have no problems unzipping the archive or chmodding the contents to 0755. I can execute the generated scripts through terminal and they perform as expected, but with PHP nothing happens!</p>

<p>I have tried specifying absolute paths for Python in my shell_exec and in a shebang within the Python file to no avail. There appear to be no syntax errors or unmet dependencies. I've tried this with all affected directories at chmod 0777 to no avail either. The section of relevant code is as follows...</p>



<pre class="lang-php prettyprint-override"><code>if (in_array($oldExtension,$allowed)) { 
    list($Width, $Height) = getimagesize($CUD);
    $EXFreshScript = $InstLoc.'/Applications/document-scanner-master.zip';
    $EXTempScript = $InstLoc.'/DATA/'.$UserID.'/TEMPSCRIPTS';
    mkdir($EXTempScript);
    chmod($EXTempScript, 0777);
    $ExtractScripts = shell_exec('unzip '.$EXFreshScript.' -d '.$EXTempScript);
    chmod($EXTempScript.'/document-scanner', 0777);
    $TempScript = $EXTempScript.'/document-scanner/scan.py';
    chmod($EXTempScript.'/document-scanner', 0777);
    $TempScriptGlob = glob($TempScript);
    foreach ($TempScriptGlob as $TSG) {
    chmod($TSG, 0777); }
    chmod($EXTempScript.'/document-scanner/pyimagesearch', 0777);
    $TempScript1 = $EXTempScript.'/document-scanner/pyimagesearch';
    $TempScriptGlob = glob($TempScript1);
    foreach ($TempScriptGlob as $TSG) {
        chmod($TSG, 0777);
    }
    $OutputDoc = $InstLoc.'/DATA/'.$UserID.'/DOCSCANTEMP.jpg';
    $Code = 'DOCSCANTEMP.jpg';
    $newCode = $InstLoc.'/DATA/'.$UserID.'/DOCSCANTEMP.jpg'; 
    $ScriptData = file_get_contents($TempScript);
    $SwapCode = str_replace($Code, $newCode, $ScriptData);
    $WriteCode = file_put_contents($TempScript, $SwapCode);
    $txt = ('OP-Act: Modified the code of '.$TempScript.' with '.$Width.', '.$Height.' on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
    $txt = ('OP-Act: Executing! '.$TempScript.' on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
    chmod($TempScript, 0777);
    shell_exec('python '.$TempScript.' -i '.$CTD);
    $txt = ('OP-Act: Execute complete! '.$TempScript.' was executed  on '.$Time.'.');
    $LogFile = file_put_contents($SesLogDir.'/'.$Date.'.txt', $txt.PHP_EOL , FILE_APPEND);
}
</code></pre>

<p>If anyone has any ideas I'd greatly appreciate it. The script this came from is over 650 lines long and I've got my issue narrowed down to this. Thanks!</p>

<p>I'll focus on this line:</p>

<pre><code>shell_exec('python '.$TempScript.' -i '.$CTD);
</code></pre>

<ol>
<li>Seems like <code>$CTD</code> is not defined.</li>
<li>Don't use <code>-i</code> flag because it enforces python prompt.</li>
<li>Try to use <code>exec()</code> instead for better debugging.</li>
</ol>

<p>If it won't help you, let me know what it outputs.</p>

<pre><code>$return = NULL;
$output = [];
exec('python '.$TempScript, $output, $return);
var_dump($return);
var_dump($output);
</code></pre>

php python permissions shell-exec