Running a .php file from the command line? - php

I want run a .php file in the command line, but I can’t.
When I get to the file and want to run was opened with IDE:
>cd c:\wamp\www\GXC-CMS-2-master\GXC-CMS-2-master
core\cms\cms.php
Should certain system settings to do?

You should do it like this:
C:\path\to\php.exe file.php

Related

execute php files without typing php in front

Anyone can assist me on how to execute my php files without preceding php front
Examples instead of typing 'php filename.php' in cli or cloud9
I want to execute them by just typing the file name
command: 'filename'
then the script runs
Well i was thinking of using the htaccess file but i'm not sure if that also works on vps or cli. I know of browser only
Edit your PHP script and add this as the new first line, by itself, before your opening <?php tag:
#!/path/to/php
(Replace that with your actual path.)
Alternatively, if you have the env command, you can use that to figure it out for you:
#!/usr/bin/env php
So your script looks like this:
#!/usr/bin/env php
<?php
echo "Hello, world!\n";
Then make the file exectuable:
chmod u+x filename
Now you can just "run" it directly in the shell:
% filename
Hello, world!
Note, you only want to do this if the script is CLI-only. If it's ever run via the web server, that first line will be output to the browser.

PHP file path from Command Line different

If I run this simple code through the browser, it works normally and creates 'text.txt' in the same location as the script.
<?php
$op = fopen('text.txt', 'w');
fwrite($op, 'test');
fclose($op)
?>
But if I run with command line
php script.php
it creates the file in a completely different directory.
Why does the same script behave differently by running the browser and the command line ?!
To fix this I need to put the full path: fopen('/home/user/site.com/subfolder/text.txt', 'w');
I have a script with many lines and many file paths, it would be too much work to change all. How can I fix this problem without putting the complete path in the file.
Open Terminal and type:
cd /home/user/site.com/subfolder
then run:
php script.php
start_script.sh
#!/bin/bash
cd /to/location/of/script
php script.php

Execute PHP routines on Windows Server [duplicate]

How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
It just opens the PHP file in Notepad.
I gave the correct user name and pwd.
Please help me..
The Run command should be
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
-f Parse and execute <file>.
I just wrote a .bat file that does the work file.bat
#ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
Run: C:\code\file.bat
Start in: C:\code\
If you running php scripts, in most of cases script awaiting to be runned in current folder.
That mean you must set up folder each your action, use field "Start In".
Example:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
Start in: C:\web\
Sounds like you don't have PHP files associated with the EXE.
You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.
Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.
This is what I did:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
#ECHO OFF
php -f d:\wamp\www\V3\task.php
This site might help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
For those people that can't able to make #Tomalak's answer work:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.php if there are included files in it.
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.
I think, you must execute your PHP script via URL.
you can write batch script for execute URL.
Why you don't write backend script in other language such as batch script, vbscript or etc.

Not able to execute batch file in php

I m trying to execute batch file in php file. I m using Apache server. Itried following ways but its not working
PHP Code
echo shell_exec('download.bat');
echo exec('download.bat');
system ("cmd /c download.bat");//Also tried for exec and shell_exec
Batch file contains downloading code using ftp client
Batch file
"c:\program files\coreftp\coreftp.exe" -s -O -site mysite -d /Export/*.* -p C:\wamp\www\file\txt
If I run it in cmd or run directly then its works fine when I run its in php its just write or echo batch file's code
download.bat file is in same folder.
I also tried to call simple bat file
start "link" "https://www.google.co.in/?gfe_rd=cr&ei=NzuIVI-FG6aG8Qef44CAAw"
Its also not calling to this bat file
Are you sure that your 'download.bat' file is in the same PATH as your PHP script?
Try to use absolute path like this
exec('C:\\MY\\PATH\\TO\\download.bat');
Just use exec('download.bat'); if the file is in the same directory, however you need to ensure that Apache has correct permissions to execute the batch file i.e. it should be running on an Administrator account. If you're on Win7 or above, look at how to run programs in elevated mode.

How to run a php script through batch file for windows 7?

I have 2 php files. One which stores various xml files in a database and other php files takes those xml files as input, parse them and produces a another table having various information about the xml files.
I want this work to be scheduled for everynight.
I want to do this via batch files but I have no idea how to use .bat files, and how to incoorporate the php script in it.
Need help/guidance.
Thanks.
Yogesh
I assume you are doing this on a windows machine as you mention a .bat file
PHP can be executed from a command line e.g. a dos shell
http://www.php.net/manual/en/features.commandline.introduction.php
Once you get it working manually just put what you are typing in a text file. (.bat)
You can execute the .bat file (a program file to windows) when you want using a scheduler, e.g. http://windows.microsoft.com/en-GB/windows7/schedule-a-task
create start.sh add this code:
#!/bin/bash
php /path/to/folder/script.php
chmod 644 script.php
OR
create start.sh add this code:
#!/bin/bash
/path/to/folder/script.php
chmod 755 script.php
the first line of your script.php has to be:
#!/usr/bin/php

Categories