# Running php from RoR

**URL:** <https://www.ruby-forum.com/t/running-php-from-ror/122193>\
**Category:** Rails\
**Created:** [December 5, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193 "2007-12-05T12:57:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/1 "2007-12-05T12:57:17Z")

</div>

Hello…

I am trying to execute a php script from within rails.

The action is triggered by a button.

def load\_stock  
if result = system(‘php test.php’)  
flash[:notice] = result  
end  
end

However, it keeps returning false.

If I run the command from the Windows command line it works.

Any ideas?

Thank you.

GPB

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/2 "2007-12-05T13:17:50Z")

</div>

backticks exist in ruby too, and you’ll get back the output of the  
command which may be useful in working out what’s gone wrong.  
The most likely thing is the the current directory isn’t what you  
think it is (ie test.php isn’t in it)

Fred

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/3 "2007-12-05T13:13:09Z")

</div>

You’re not telling us what environment you’re running this under.

It’s possible that you’re running into issues with permissions,  
apache, or otherwise. Also, if you run “system”, you won’t get a result.

The running environment is temporarily replaced while the system call  
executes. I assume you’re trying to get the perl-like behavior of:

$foo = `some command`

Rails isn’t going to work in the same way.

-j

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/4 "2007-12-05T13:47:14Z")

</div>

Hello John,

Thank you for replying.

I am running this under a Windows 2003 Server, using Mongrel, Ruby on  
Rails 1.2.5.

I am interested in getting the php script running, not so much  
returning a value. The php script is supposed to run some queries  
against an AS/400 to update a table. I would like to do this only in  
Rails but I couldn’t get the ODBC connector to work, it seems it  
doesn’t support the AS/400 database system. In the past I have been  
successful doing this with PHP, so I’m trying this approach of  
triggering the php script from within the Rails application.

Thanks,

GPB

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/5 "2007-12-05T14:00:12Z")

</div>

Hi Frederick,

Thanks for replying.

I have also tried…

```
result = system('php c:/test.php')

```

Same problem. This runs on the command line, but not when called from  
RoR it returns false.

But I went with your advice and started to use backticks and at least  
I got something back when running the windows command “ver”.

So I think I am on the right track. Will keep trying.

Thank you.

On Dec 5, 1:17 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![GPB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@GPB](https://www.ruby-forum.com/u/GPB)\
**Post date:** [December 5, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/running-php-from-ror/122193/6 "2007-12-05T15:17:10Z")

</div>

Hi Frederick,

OK, backticks did the trick. Thank you.

But it seems Ruby doesn’t find php.exe even though it’s in the Windows  
path, so I need to call it using the windows path.

result = `c:/progra~1/php/php c:/test.php`

Thanks again for your help.

GPB
