Python script not executing properly from Php - php

I'm trying to run a python script with Php but it's not running properly when I launch it with Php. This script, here "bot.py" interracts with the instagram api and writes the output as a text file in 'bot1.txt'.
Here is the Php code :
shell_exec("python automation/bot.py");
And here is the python snippet that writes in the file :
with open('../botlog/bot1.txt', 'a') as the_file:
the_file.write('Followed')
My problem is that when I launch it with Php the script runs but doesn't write in the file bot1.txt. When I run the python script with the command line everything works perfectly so I don't understand what's the deal here.
I'm working with MAMP on a local server.
Thank you !

Maybe you should specify your path to php.
$myPHPScriptPath = realpath(dirname(__FILE__));
Then specify the relative path to your python script.
Hope it helps

Related

Why does this PHP script with a shell_exec call run from the command line in Windows 10, but not the browser/localhost?

I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests

python script executed from php won't read saved model

Here is what I'm trying to do:
I'm sending an Ajax request with an image as data to a php script that uploads it and execute a python script with the image name the python script loads a gender detection model and predict the gender on the person on the image.
The problem is the model doesn't load, I tried it with the php and python script in the same directory and it worked fine but I'm trying to use it in laravel so I can't do that anymore.
when I try running the python script manually like that :
python path/to/script.py
It throws this error : OpenCV(3.4.1) Error: Unspecified error (File can't be opened for reading!)
but when I do this it works:-
cd path/to/script.py ; python script.py
Here is my php:-
$file = $this->upload_Image($request->get('image'));
$path =public_path().'/app/Http/Controllers';
$command='cd '.$path.';';
$path= 'python '.$path.'/facifier.py '.storage_path('app').$file;
$command .= $path;
return exec($command);
Storage::disk('local')->delete($file);
return json_encode($orderedData);
The command ends up like this : cd absolute/path/to/script.py ; python script.py absolute/path/to/image.jpg
when I use it on a command prompt it works fine.
Here is where I get the error on my python script:-
fisher_face_gender = cv2.face.FisherFaceRecognizer_create()
fisher_face_gender.read('models/gender_classifier_model_kdef.xml')
does anyone know whats causing this, I'm on windows by the way.
thanks to bruno desthuilliers comment i got it working. The problem was in the path to model i used,its relative to the python script because i thought that the script executes on its own path not on the command promt's path .
I just changed the path to the model to the absolute path and it worked fine

Octave script through php windows

I am trying to run an octave script through PHP. I already googled and found some results but none of them are working for me. I tried with exec() and system(). I even created a batch file which calls 'octave myScript.m" and called this bat file using system() of PHP but it doesnt seem to work. In the browser page I am just seeing 'C:/FOLDER_PATH>octave myScript.m". The octave script simply creates a new file and writes some text to it. When i directly run the bat file (by double-clicking on it), the file is getting created properly. I also added folder path to octaverc file but it doesnt seem to work. I need to do some image processing in octave for which I already wrote the script. I need to invoke this script on a client request and send the result to back the client. I am checking the invocation process through a sample script which as I mentioned earlier creates a new file. What am I doing wrong?
My php code is this:
$cmd = "cmd /c C:\PATH_TO_BAT_FILE\myBat.bat";
exec($cmd,$output);
system($cmd);
echo implode ("\n",$output);
Note that my path contains double backslashes to avoid escape sequence characters
My bat file is this
octave temp.m
My octave code(temp.m) is this
fid = fopen("helloScript.txt",'w');
fprintf(fid,"Hello world!");
fclose(fid);
Ouput on the webpage is this:
C:\PATH_TO_BAT_FILE>octave temp.m C:\PATH_TO_BAT_FILE>octave temp.m
I can see in the task manager that a new process is getting created whenever I run the PHP script in browser (I am guessing that it is cmd).
Also, when i change my bat file to
echo hello
I am able to see the following in my browser page
C:\PATH_TO_BAT_FILE>echo hello hello C:\PATH_TO_BAT_FILE>echo hello hello
So this could mean that the bat file is getting executed properly. But when I replace the bat file script with 'octave MY_FILE.m' I am not able to see the output. It may mean that my octave is not configured properly? or is there something I am missing?
Any help would be appreciated.
Thanks
If you are going to run the batch file to create it in php then the php command should be like this.
exec('cmd.exe /c C:\path\to\test.bat');
This is embarassing but I solved it by giving full path. In the bat file I specified the complete path of the octave.exe (C:\Software\PATH_TO_OCTAVE.EXE) and the complete path of the '.m' file.
In my php, I just used exec()

How can I have a php script call a perl script that requires a parameter?

I have a php script that allows people to upload files to a server, and I have a perl script to convert the filetype to something more useful for data extraction. The php script saves the name of the uploaded file as $filename . In order to run, the converting script needs to be passed the name of the file to be converted. I was able to call a test script and pass it the name of the uploaded file in the following way
exec("/usr/bin/perl /full/path/to/mytestscript.pl $filename");
This test script executed all of the commands that I needed it to. However, this did not work with my converting script. The code to keep the variable is the same in both the test script and the converting script.
I can run the converting perl script fine from the terminal, but I want to run it from the php script. I run the converting perl script from the terminal by navigating to the directory holding both the file to be converted and the converting perl script.
perl my_converting_script.pl file_to_convert.TCX
Any advice on how to have the converting script run from the php script instead of the terminal would be greatly appreciated!

PHP exec python not working

hey yall. Im running python on a webserver from dreamhost. I am using their install of python and am using a lastfm module that can be found here: http://code.google.com/p/python-lastfm/
to get it to import properly i do this
import sys
sys.path.append("/home/myusername/build/Python-2.5/Lib/site-packages/")
import lastfm
since the lastfm module is installed there.
When I use putty to ssh into my server, i can simply run python test.py and it works perfectly. But when i run it from a php script with
exec("python test.py");
it suppossedly does not work and the script doesnt run. it runs perfectly fine when i do
import lastfm
and then have other things after,
but when i actually try to do something with the module like:
import lastfm
api=lastfm.Api(api_key)
it does not run. once again i can run the script using the same python install in a shell and it executes fine. So something must be happening that goes wrong when i run it from the php script. I figured it would be running the exact same python and everything. I checked other posts and they say it may be something with file permissions, but ive put every file to 777 and it still doesnt work. idk what the problem could be. thanks in advance everyone.
Try using the full path to the python executable. For example:
exec("/usr/bin/python test.py")
You can find the full path from the command line using the which command:
$ which python
/usr/bin/python
Whatever error python is raising would be going to the child's stderr. Try either telling php to read from stderr, or (in python) do this:
import sys
sys.stderr = sys.stdout
For Windows users:
$output = null;
exec('C:\\Python27\\python.exe C:\\sud.py', $output);
echo var_export($output, TRUE);
The code i was searching whole day ^^
That's why - hope it'll help somebody.
For Windows User -
Thanks to Karlisup my PHP file could read python.
I'm using BITNAMI WAMP in EC2 Amazon, my python file (leadatos.py) and php file are on htdocs folder.
My calling was
<?php
passthru('C:\\Python27\\python.exe leadatos.py');
?>
The last line of my Python file was print "message".
Hope it words!

Categories