PHP executing script in subfolder

Go To StackoverFlow.com

2

I've run into a slight issue with the way my site works. I have a python script which I want to run from the index.php page. Now if this script is in the root directory, by using this command I can execute it easily:

<?php
exec("myscript.py");
?>

This actually runs the python script and does what is needed. However, I am planning to have a few more script and want to keep the root dir as clean as possible, hence I was wondering if it is possible to execute this script from a subdir within my root?

Current Setup:

Root -  C:\wamp\www\homepage\
Python Script Folder - ...\homepage\python\

When i put my scripts in the python folder, no matter what I use, the php does not execute it.

Tried:

<?php
exec("/python/myscript.py")
?>

<?php
exec("//python//myscript.py")
?>

<?php
exec("\python\myscript.py")
?>

<?php
exec("\\python\\myscript.py")
?>

<?php
exec("python/myscript.py")
?>

<?php
exec("python\myscript.py")
?>

<?php
exec("../python/myscript.py")
?>

All this and nothing launches the darn thing. What am I doing wrong :[ Dammit!

2012-04-04 19:49
by Meh
exec("python/myscript.py") should work. But try exec(__DIR__ . "/python/myscript.py")binarious 2012-04-04 19:51
@binarious - both seem to fail... : - Meh 2012-04-04 19:54
maybe since its in a folder, the python script itself is failing. Maybe make a simple py script and output some text. I am not sure what the python script is doing nor have I ever ran py from php so this may not be helpful at all : - Ronnie 2012-04-04 19:57
Ronnie - The idea is perfectly logical, but if I can run the script without errors manually within the subdir, i do not see why It can not run when php executes it. Thank you for your input though : - Meh 2012-04-04 20:00
may be path issue.. did you try to execute a php file first then blame p - zod 2012-04-04 20:04
What you could do is make a main python script that runs all of the other scripts you need. Could that work - IT Ninja 2012-04-04 20:25
+1 for path issue - does your script do anything depending on the working directory? Also, if it was linux, I'd say look at your permissions - I'm not so sure how that might work in Windows, but perhaps your script can't execute as the Apache user doesn't have permissions in that folder (like I said, really not sure how that works in Windows - Aerik 2012-04-04 20:37
try specifying full path: <?php exec("C:/wamp/www/homepage/python/myscript.py") ?>apple16 2012-04-04 22:22
Upvote for the image of you flipping your desk over! : - murftown 2013-07-12 23:09


1

Did you try calling the python executable directly? It worked for me. I had a myscript.py like this:

print 'hello'
print 'world'

and then put it in a directory called temp/.

My php is in a file called temp.php as follows:

<?php
exec('python temp/myscript.py',$output);
print_r($output);
?>

Then I ran it and here's what happened:

$ php temp.php
Array
(
    [0] => hello
    [1] => world
)
2013-07-12 23:09
by murftown
Ads