Python file will not run from php - php

I have a PHP file on my web server that is supposed to call a python script (same folder) that writes out to a file and then get its contents and displays the data.
The PHP file is called q.php
it contains
<?php
$tmp = exec("trivia.py");
sleep(4);
$homepage = file_get_contents('./testfile.txt', true);
echo $homepage + '<p>exec ret:' + $tmp;
echo exec("whoami");
?>
This file calls a trivia.py which writes out a file ("./testfile.txt") and then php gets the data from the file and displays it. I added a variable to see if the exec is working and it returns 0. The PHP server is being executed by user http.
now for trivia.py, I have the following line at the top of the file
#!/usr/bin/env python
and it executes perfectly fine when I SSH into the server. From SSH I run the script and it creates the file specified above and the web page works fine. However, if I used the PHP file to create it, it will not work from the web.
I am pretty sure this has something to do with permissions somewhere but I am not that great on permissions for Linux.
SYSTEM INFO: Synology Diskstation, DSM5, PHP5, Python 2.7
EDIT:
trivia.py currenlty has 777 as permissions with admin owner and group users

When running a file via exec you need to provide the full path to the file, it doesn't matter if the file is in the same directory.
Please try it with (assuming the file is in /var/www/public)
exec('/var/www/public/trivia.py');

For Windows User -
Problem was resolved here PHP exec python
I suggest to replace $tmp = exec("trivia.py"); for this one
$tmp = exec("C:\\Python27\\python.exe trivia.py");
// Add your python.exe route before the file
Hope it works!!

Related

Passing arguments dynamically to a batch file by using php

Here is my batch file script. I would like to pass arguments dynamically to the batch file. Based on the file name as part of the query string, it should copy that specific file to the network folder.
Echo off
set arg1=%1
net use \\windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed\%arg1% \\windows_servername\\test_folder
my PHP file code is below
echo $fname = $_GET['FILE_NAME'];
system("cmd /c C:\\scripts\batch1.bat ".$fname);
I am getting out put as The command was successfully completed. But file was not copied to the network folder.
But It is working fine, when I gave the file name directly on batch file like below.
xcopy C:\Apache\htdocs\Invoices\invoice_feed\sample.csv \\windows_servername\\test_folder
Any help would be greatly appreciated.

How to execute the python script in php with different path?

I have a php file which store in C:/localhost
try.php
<?php
echo exec('python resizer.py');
?>
Above code only allowed me to run the python script that store under C:/localhost
My problem now is I not able to run the script that store in different path from the php file.
For example now my resizer.py is stored in C:/Hello/images, and my php file is stored in C:/localhost, so how i going to execute the python script in php file in different path?
Can share me some ideas? Please help, thanks.

file_get_contents failure on windows

I have a PHP CLI application that creates a file with file_put_contents then listens for changes to that file. If the filemtime changes then I try to get the content with file_get_contents. It often fails to retrieve the contents on windows. This baffles me. How is it possible that a process that creates the file cannot open the file?
I even ran icacls on the folder that the file is in and it still fails to have access to read the file that it created.
icacls.exe 'MYFOLDER' /grant MYUSER:(OI)(CI)F /T
Can someone please enlighten me on how to insure a PHP process can read a file it created?

File not created (Linux-PHP-C++)

I am trying to save a file to the current directory in Linux and then display it on a webpage. Currently I run a C++ executable from a php script with the following code
exec("/var/www/html/radsim/plotFluence $rMin $rMax $zMin $zMax $lum $graphStyle $basepath[$path]", $return);
When I run the executable from the console in Linux the file is created fine, the problem arises when I try from within the php; the file is simply not in the directory . The user inputs values and the executable is run but no file is made. The C++ looks like this
canvas->Print(("/var/www/html/radsim/"+histoName+_FileFormat).c_str());
The permisions are set to 777. In addition, on another PHP script, I use fopen("data.txt", 'w') or die() to create a text file, but it always dies.
Seems like a sandbox. There must be a php config option - best to start here: http://php.net/manual/en/configuration.changes.php

Windows Batch File To Execute PHP Accessing mySQLi Database

I am writing a windows batch file that needs to execute a PHP file which fetches data from a backend and inserts into mysql database.
Below is the code I used and it is working but it will open the browser.
#ECHO OFF
START http://localhost/test.php
How do I ensure that the browser is not invoked when START is executed? I have tried to put /B at the back but it not working.
I have also tried the following but it is not working at all and nothing gets inserted.
#ECHO OFF
php.exe -f "C:\wamp\www\test.php"
The simple answer is that php files are set up to open with your default browser, when you open the php file, it will open with that browser.
If you want to view the contents of the file in cmd instead you can use
type test.php

Categories