Running External Scripts from CodeIgniter |
[eluser]cdicelico[/eluser]
Hello, I have an application I'm working on and am trying to figure out how to get a crucial script to run. I have tried using exec() and passthru(). They both are allowed on my server and they both work with any command except executing my script. I've also made the script executable, added the shebang to it, and tried every other suggestion I could find in the PHP forums but I'm still not able to get my script to run. Are there any caveats to doing this? For example, if you have a script written in Ruby or Perl or whatever and you wanted it to run with input from a CI form, what's the best way to go about doing that? Thanks!
[eluser]jáquer[/eluser]
I call a python script from one of my apps. I call it like so: Code: exec(dirname(__FILE__) . '/gv_sms.py ' . escapeshellarg($number) . ' ' . escapeshellarg($data)); It's important to use the full path to the file because it might not be present in the $PATH.
[eluser]cdicelico[/eluser]
Thanks! This is an awesome technique, I'm going to try it. Do you host your non-CodeIgniter scripts inside the applications directory or outside it? Where in relation to the controller where you're calling the script do you have your python script? Thanks again.
[eluser]jáquer[/eluser]
In this particular case it was a very simple script so I just placed it in the helpers directory. I wrote a helper with a single function to call the script, as above.
[eluser]cdicelico[/eluser]
Yeah, mine is really a collection of scripts that depend upon each other and on installed libraries and programs on the server, so I naturally put them all in their own directory. But, the helper idea is really good, I should try that approach. The arguments $number and $data above, are they coming from a form where the user inputs data? Thanks for the help.
[eluser]jáquer[/eluser]
Yes, the data comes from a form, then within the controller I load the helper and call it: Code: $this->load->helper('gv_sms'); Leaving your script on its own directory sounds fine too, just make sure you specify its full path when you call it.
[eluser]cdicelico[/eluser]
Okay, I am really at my wits' end here. I set up a two files: test.py and test.php. Here are the contents of test.py: Code: #!/usr/local/bin/python2.5 and here is the test.php page. The directory structure of the application is like this: -application (views, controllers, models, etc.) -system -wavescripts (python code) -uploads -js -css -img Now, if you look at the test.php page, you can see that the view is executing from the views dir, anything executed via Code: exec() Code: exec() Code: exec() Code: cat Code: exec() Code: escapeshellarg() Any ideas on what is going on here would be greatly appreciated.
[eluser]jáquer[/eluser]
Read the documentation for exec(). It will only return the last string of the output. If you want to see the entire output then use passthru(). Also, read up on escapeshellarg(). It's meant to be used around the arguments you pass to the external script.
[eluser]cdicelico[/eluser]
Yeah, I read all that and I already tried using passthru(). In fact, that was my initial setup. However, nothing at all got returned, so I tried exec() instead. Oh, well, thanks again.
[eluser]jáquer[/eluser]
This page you linked is definitely working. Just replace all exec() calls with passthru() and you'll see the whole ouput, including the "Hello, World!" line. |
Welcome Guest, Not a member yet? Register Sign In |