Double click and run the Php script on mac - php

I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.

Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php

I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.

To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php

You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".

Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.

Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.

Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.

Related

PHP external program call

My setup is as follows: Windows 7, XAMPP with Apache and PHP enabled I have a PHP script in which I call an external program to do run a conversion. This external program is an EXE file, which requires 3 attributes:
The source file
The destination file
Additional flags (conversion type etc)
When I use the command line tool built into XAMPP to execute my script, everything works fine. But when I use the exec() function in my PHP script, no output file is created. I'm pretty sure the conversion is actually happening (it takes about 5 seconds, about the same time it takes to run the PHP script).
I think it's a permissions thing, so I already moved the EXE file to the same folder as my PHP file and adjusted the permissions of the entire folder (I granted all permissions to all users). I also disabled the Windows UAC and tried to put the command in a BAT file. The file just is not created.
Any help or tips would be greatly appreciated!
My PHP code is as follows:
exec('c:\converter.exe c:\src.txt c:\dst.txt -f', $output);
print_r($output);
When I print out $output, the array turns out to be empty. When I put the exact same command in Command Prompt, the code works like a charm (no syntax errors). I use absolute paths as well.
Try to copy your executable file in same folder as your application.
try
exec("script.exe src.txt dst.txt", &$output);
echo $output;
also, do not forget to use escapeshellcmd() to add some security to your application.
Thank you very much for your input! As it turns out, it was Windows issue caused by the 'Interactive Services Detection' feature. Apache was running as a system service, which prevented calls to external programs (with a GUI). I disabled the run-as-service feature in XAMPP, which solved the problem. A more thorough explanation can be found here: http://php.net/manual/en/book.exec.php

Run .php file in Windows Command Prompt (cmd)

I want to run a .php file in windows cmd.
I followed this (suggestion)
php is not recognized as an internal command (in Windows)
But it did not work.
I tried this command prompt: C:\Windows\system32> cd \myfolder
And within myfolder I did as follows:
C:..\myfolder> php file.php
but I get an error "php is not recognized as internal or external"
But when I try without php command like this; C:..\myfolder> file.php
it just opens in notepad, yet I want to run it in cmd. How can I do it?
If running Windows 10:
Open the start menu
Type path
Click Edit the system environment variables (usually, it's the top search result) and continue on step 6 below.
If on older Windows:
Show Desktop.
Right Click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the System Properties window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
Note: Make sure that your PHP folder has the php.exe. It should have the file type CLI. If you do not have the php.exe, go ahead and check the installation guidelines at - http://www.php.net/manual/en/install.windows.manual.php - and download the installation file from there.
you can for example: set your environment variable path with php.exe folder e.g c:\program files\php
create a script file in d:\ with filename as a.php
open cmd: go to d: drive using d: command
type following command
php -f a.php
you will see the output
You should declare Environment Variable for PHP in path, so you could use like this:
C:\Path\to\somewhere>php cli.php
You can do it like this
It seems your question is very much older. But I just saw it.
I searched(not in google) and found My Answer.
So I am writing its solution so that others may get help from it.
Here is my solution.
Unlike the other answers, you don't need to setup environments.
all you need is just to write php index.php if index.php is your file name.
then you will see that, the file compiled and showing it's desired output.

How to run php files on my computer

Could anyone please tell me how to run a php file locally on my system.
Currently I am using a server to run files.
I know both php & Apache to be installed.
I need to see out put of this program, for example:
<?php
$a=5;
$b=10;
$c=$a+$b;
print $c;
?>
Can you please tell how I can run these files of if I need anything more.
php have a easy way to run a light server:
first cd into php file directory, then
php -S 127.0.0.1:8000
then you can run php
You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.
What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).
Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.
Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.
In short:
Install WAMP
Put this file to C:\wamp\www\ProjectName\filename.php
Go to browser: http://localhost/ProjectName/filename.php
I just put the content in the question in a file called test.php and ran php test.php.
(In the folder where the test.php is.)
$ php foo.php
15
If you have apache running, put your file in server folder for html files and then call it from web-browser (Like http://localhost/myfile.php ).
Running PHP script directly in browser:
Here are all steps (in short) to run PHP program in XAMPP
Step 1: First of all, open the Apache Friends website, The download and install XAMPP for Windows.
Step 2: Open the XAMPP Program Control Panel and start “Apache Web Server”.
(NB: If your PHP scripts need MySQL database to work, Start “MySQL” service as well)
Step 3: Create a new folder inside “htdocs” folder. For example: “php-project”
Path looks like: C:/xampp/htdocs/php-project/
Step 4: Open any text editor and create a new file and write a simple PHP program then Save it with .php extension as “example.php”.
Path looks like: C:/xampp/htdocs/php-project/example.php
<?php
echo “Hello Level 5 Developers”;
?>
Step 5: Go to the browser and type “localhost/php-project/example.php” in the address bar and press enter then it gets executed.
Step 6: After that, you can see the output of your first PHP program like this:
Hello Level 5 Developers
3 easy steps to run your PHP program is:
The easiest way is to install MAMP!
Do a 2-minute setup of MAMP.
Open the localhost server in your browser at the created port to see your program up and runing!

PHP script on my MAMP testing server can't access node_module: less

I'm trying to configure my local development environment to read .less files so that I can edit .less files during development and only convert to .css when it's time to go live. Make sense?
I'm running MAMP as my local testing server. I'm following the instructions I found here:
http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920
In short, the plan is to use an htaccess file catch requests to .css files and direct them to a PHP script which compiles the .less file of same name and returns the css code.
Everything seems to be working from the command line. I can compile a .less file from the command line and it spits out the css. I know my rewrite rule is working because I can type the url into a browser and see the output of my php script. For example, if my PHP script calls echo shell_exec('pwd'); I will see a path printed in the browser.
THE PROBLEM is that I can't get the less script to run unless I SSH to the localhost as root. When I exit SSH and run the command I get "Permission denied". I suspect this is what happens when my PHP script tries to call this... so it's returning nothing.
I guess the question boils down to how can I get my PHP script to run the less compiler?
UPDATE! I solved the problem...
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work.
There are a lot of ways to sidestep this, but I determined that editing the list of sudoers with sudo visudo was the best for my purposes. There was a lot of helpful tips on this post. Through trial and error, I figured out that PHP uses the www-data account. Adding this line fixed my problem:
www-data ALL=(ALL) NOPASSWD: /var/root/node/npm/node_modules/less/bin/lessc
Something to remember is that you STILL have to add sudo to the command that gets fed to shell_exec(). Hope this is helpful to someone else.
Maybe it would be easier if you'd use the PHP implementation of lesscss: lessphp
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work...
See my edits to the question above.

how to give command line to run browser which in turn runs php script

question is how to give command line (in .cmd script) to execute browser to then in turn have browser execute .php script.
Presently if from a command window I execute like ...
"c:\Program Files\Internet Explorer\iexplore.exe" file:\c:\users\win7ultsdtest\findroot.php
....OR....
C:\Program Files\Internet Explorer\iexplore c:\users\win7ultsdtest\findroot.php
This will run the Explorer browser, but then the browser the browser will download the contents of findroot.php instead of executing the php code as I need. Does anyone know how I can get the browser to instead execute the php code and not just download it as data?
Let me explain my need ... The findroot.php file contains php code to access the $_SERVER['DOCUMENT_ROOT'] variable. This variable is ONLY non-null when the localhost is running a http server and then it contains the localhost document server root path where loadable browser .html, .php etc may be stored to loaded from http:\ lines.
The findroot.php outputs the $_SERVER['DOCUMENT_ROOT'] contents to a file as rootpath.txt so that my .cmd script can then can automatically install PHP code into the active PHP servers document root area.
So understand I must find the $_SERVER['DOCUMENT_ROOT'] from a .cmd script.
Now I might search ALL the computers drives for httpd.conf and then scan that file for the value but this wouldn't work for two reasons; 1. there can be multiple httpd.conf files and I can't know which server is active and using what httpd.conf. 2. it would take a long time to search a given computers entire drive(s) on all httpd.conf files.
The browser won't execute PHP code. You either need a server to run PHP and to access it via HTTP such as C:\Program Files\Internet Explorer\iexplore http://localhost/url/for/findroot.php or you can run PHP via the command line c:\path\to\php.exe c:\users\win7ultsdtest\findroot.php. However running it via the command line won't give you $_SERVER['DOCUMENT_ROOT'] as that is only populated when running PHP within a server.
It's not possible to execute PHP by the HTTP server without the server knowing about the PHP before hand - for example, by being in the document root. Of course if you don't know the document root, your script won't be there. If you're trying to install a script into a web server for a user, it's much better to give instructions on how to do so as their server environment will likely vary from what you expect.
There are light browsers like lynx if you are in a linux machine
lynx http://whateverurl/php.php

Categories