How to call same program from PHP on different OS's?

Issue

I have a LAMP stack that I am developing a web app that will be used on different servers running different flavors of linux and mac OS.

I want to use the exec() function of php to call the soffice program to convert an ODT to a PDF. However, when you make this call, you have to use the full file path of the program.

On linux it is typically under /usr/bin/soffice, but on Mac it’s under /Applications/LibreOffice.app/Contents/MacOS/soffice.

So my question is, what is the best way to deal with different program locations for the same function call?

I thought about setting a PATH variable on mac and linux in the OS and then just call exec(soffice), but apparently the OS PATH is not transferred to the PHP getenv[‘PATH’].

What is the best way to accomplish running the same program on different platforms which put the same program in different locations?

Solution

on both linux and mac you can run $(which soffice) my-args-here and get the full path to the executable on the current machine.

The other option is to detect on which OS you are currently running and then fire the command with the correct path.

Answered By – Broshi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published