I'm trying to run a php file within a batch file. I have the folder for php.exe within the same directory as the bat.
set addLocation="%cd%\test.php"
set exeLocation="%cd%\php\php.exe"
start "run" "%cd%\php\php.exe" "%location%"
pause
Any help would be appreciated
I use this method myself
Batch file called xxxx.cmd
PATH=%PATH%;C:\path\to\folder\with\phpexe
CD \path\to\php_source
php file.php
The php folder is added to the PATH only for the duration of this command windows existance
You can follow the following code:
To change the directory -
set path = D:\Workspace
CD /D %path%
To run the php file -
php file.php
This code will work well.
Thank you.
Related
I want to run ~/bin/git_cache_meta.sh in a folder ~/www/project/folderA/ with a php script.
My script is located in the www folder and looks like this:
#!/usr/bin/php
<?php
chdir('project/folderA/');
exec('git_cache_meta.sh --apply');
The script shows the error sh: 1: git_cache_meta.sh: not found how can I tell exec that the script git_cache_meta.sh is in the folder ~/bin/git_cache_meta.sh ? I do not want to copy the file git_cache_meta.sh to ~/www/project/folderA/.
You should use full path of the file and be sure it has the necessary access.
i.e.
in your case:
~/bin/git_cache_meta.sh
Also, use chmod +x to give it the permission to be executable.
I have a php script that should run a command located in a certain subfolder, in another subfolder.
The command is in folder Cmd, and should be executed in folder 1.6.2. So first I switch to the 1.6.2 directory, and then I use a relative path to call the command:
exec("cd 1.6.2");
exec("..\Cmd\sencha app build production");
But this throws the error that the directory couldn't be found, because the second exec still executes in the main folder, where the calling index.php file resides.
The php manual on exec doesn't provide any possibility to execute in another directory. Am I missing something here?
The current system is a Windows, but I have to make it portable because it may be executed on linux in the future.
Try changing the working directory first
e.g.
chdir('/path/to/1.6.2');
exec("sencha app build production");
You have to change the directory within the same exec() command.
like so: exec("cd 1.6.2 && Cmd/sencha app build production"); depending on your actual folder structure. I'm just guessing)
I have a PHP file "install.php" which handles the installation of other scripts. I want this file to delete itself and its containing folder after it has been run.
Is this possible?
A demo given that the folder only contains your install.php:
mkdir demo
cd demo
echo "<?php unlink(__FILE__); rmdir(__DIR__); " > install.php
php install.php
cd ..
ls
The that ls doesn't show the "demo dir" any more.
The recursive deletion shouldn't be that hard to figure out ether if there are more (sub-)folders you want to remove.
You can use PHP unlink and rmdir command to delete the file (and its folder) itself. Make sure you forward the viewer to other page after you delete the file itself.
I'm trying to setup a PHP file as a cron job, where that PHP file includes other PHP files.
The file itself is located at /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/runner.php
The include file is at
/var/www/vhosts/domain.com/httpdocs/app/protected/config.php
How do I include that config file from runner.php? I tried doing require_once('../../config.php') but it said the file didn't exist.. I presume the cron runs PHP from a different location or something.
The cron job is the following..
/usr/bin/php -q /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/runner.php
Any thoughts?
Your cron should change the working directory before running PHP:
cd /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/ && /usr/bin/php -q runner.php
Note that if the directory does not exist, PHP will not run runner.php.
You should use an absolute path. The cron is probably not running the script from within the directory in which it resides.
I recommend using:
require_once( dirname(__FILE__) '../../config.php )
__FILE__ is a special constant that refers to the file you're in. dirname(...) will give you the directory, which will evaluate to the absolute path of the file you wish to include.
Is classes or cron a symbolic link? I seem to remember that php evaluates the real path instead of the symbolic path.
Consider the following directory tree:
/target/index.php
/path/sym -> /target
if you were to execute php index.php from /path/sym then the statement require_once('../require.php'); would evaluate to require_once('/require.php'); not require_once('/path/require.php');
You can change the working directory inside your PHP script to the location of that script:
chdir(__DIR__);
(or, if your PHP version is before 5.3: chdir(dirname(__FILE__));)
Then you can do:
require_once('../../config.php')
I need to run a scrip via command line but the files necessary for me to run it require me to be in the directory of the script. But I cannot do it for every command line i need to run. Their are over five thousand. Could someone tell me how to either format the list easily or add something to the format that would make it run. I have something like this....
php /path/to/the/script/01240/script.php
php /path/to/the/script/03770/script.php
php /path/to/the/script/02110/script.php
php /path/to/the/script/02380/script.php
php /path/to/the/script/03220/script.php
php /path/to/the/script/02340/script.php
php /path/to/the/script/03720/script.php
php /path/to/the/script/03460/script.php
php /path/to/the/script/0180/script.php
php /path/to/the/script/02000/script.php
php /path/to/the/script/01830/script.php
php /path/to/the/script/0980/script.php
php /path/to/the/script/0400/script.php
php /path/to/the/script/02750/script.php
php /path/to/the/script/0760/script.php
php /path/to/the/script/02690/script.php
..... and it goes on for 5000 more lines.
find -type f -iname script.php -execdir php {} \;
Or, if the scripts are differently named:
find -type f -iname '*.php' -execdir php {} \;
Edit: If it's a list of specific scripts rather then all:
An alternative would be to define an auto_prepend_file in your php.ini (or a custom php.ini for this script), which would enable you to put there:
<?php
chdir(dirname($argv[1]));
?>
You don't need to edit your list by hand. Write another script which reads in your main script, and adds cd commands before/after each one. Then run the result of that.
You can create a new file called php1.bat with the following content:
pushd %~p1
php %1
popd
This will change to the directory of the parameter, execute php and jump back.
After this, replace every occurence of php / in your script by call php1 / with your favorite search/replace all editor, so it doesn't execute php, but php1.bat.
I can suggest two possible solutions:
One is to add an option to your script so you can specify the working directory. Each process has its own working directory, so this would mean your PHP script changes its directory but the shell you are running the script from does not. Once the PHP script finishes, you're back in your shell in the same directory you started from.
<?php
$options = getopt("d:");
if (isset($options["d"])) {
chdir($options["d"]) or die("Cannot chdir to " . $options["d"]);
}
...do the rest of the script...
Then invoke your script as:
php script.php -d /path/to/the/script/01240/
The other solution is to change directory as you invoke your PHP script. Remember I said each process has its own working directory. But you can make the shell open a sub-process simply using parentheses. Then use cd in that subshell to change directory and invoke the PHP script. Once you finish the subshell in parens, you're back to where you started.
shell$ ( cd /path/to/the/script/01240/ ; php ~/bin/script.php)
shell$ ( cd /path/to/the/script/03770/ ; php ~/bin/script.php)
shell$ ( cd /path/to/the/script/02110/ ; php ~/bin/script.php)
But I'm guessing that your script is simply opening files with a relative pathname. If you literally do some code in the script relative to dirname(__FILE__) which is the location of the invoked script, then these solutions won't work.