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

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.

Related

Python file will not run from 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!!

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

Executing PHP script via SSH

I want to send data to my server via SSH. The data is image files that need to be saved into directories on the server.
If I run the script via SSH how can I get a PHP script to read the image files?
For example, if I used bash I could do
./uploadimage.bash -image.jpg
and that would be that. But my knowledge of PHP is limited. Usually PHP uses the $_FILE array when dealing with files, as far as I know. Do I need to use this if I send the files by SSH?
Run the script via php command line executable:
php myscript.php image.jpg image2.jpg
You can the get the file names via $argv array and deal with them any way you please.
It depends on what you need to do with the images.
Do you just need to echo them out to the user?
echo file_get_contents('image.jpg');
Are you meaning to retrieve the command-line variables passed to the script? Use $argv.
myScript.php image.jpg # image.jpg becomes $argv[1]
Do you need to do processing on the images? Use the GD functions.
$image = imagecreatefromjpeg('image.jpg');
// Do processing
imagejpeg($image); // Pass a filename if you want to save it instead of output it.
If you want to simply upload a bunch of files to a remote server, your best bet is to use scp command, rather than PHP
scp /your/file.png username#host:/remote/path.png
If you execute a PHP script through an SSH Session, there's no way you can send a file through SSH. You have to first copy it to the remote server and then execute the PHP which uses file_get_contents or something like that.
If you are sure that PHP must be the recipient of the file (maybe you want to do some logic to determine the file name, path), you have to host it as a webserver, and you can use curl to upload the file, like this:
curl -k -F myfile=#image.png foo.php.

Run PHP in memory

As I can't write file on the GAE server, is there a way that I can directly run PHP code in memory without the help of a php file.
BTW, I was using Quercus to run PHP in GAE.
You can refer to the link: http://www.webdigi.co.uk/blog/2009/run-php-on-the-google-app-engine/
Thank you so much.
You can use eval() from PHP to execute a string of PHP code without saving it to a file.
For example:
eval('echo hi;'); // this echoes "hi"
You can upload the file, you just can't create one using code
So you can upload the PHP files you want with the Quercus stuff, and access the files, but the Python/PHP/Java code being run on the server cannot create files
I don't really see why you want to use a dynamic PHP file...

Categories