[php] Execute a PHP script from another PHP script

How would I make my server run a php script by triggering it manually using php? Basically I have a pretty big cronjob file that is ran every 2 hours, but I want to be able to trigger the file manually myself without having to wait for it to load (i want it to be done on the server's side).

EDIT: I want to execute the file from a php file... Not command line.

This question is related to php

The answer is


Try this:

header('location: xyz.php'); //thats all for redirecting to another php file

On the command line:

> php yourfile.php

you can use the backtick notation:

`php file.php`;

You can also put this at the top of the php file to indicate the interpreter:

#!/usr/bin/php

Change it to where you put php. Then give execute permission on the file and you can call the file without specifying php:

`./file.php`

If you want to capture the output of the script:

$output = `./file.php`;
echo $output;

Possible and easiest one-line solution is to use:

file_get_contents("YOUR_REQUESTED_FILE");

Or equivavelt for example CURL.


If it is a linux box you would run something like:

php /folder/script.php

On Windows, you would need to make sure your php.exe file is part of your PATH, and do a similar approach to the file you want to run:

php C:\folder\script.php

Open ssh and execute the command manually?

php /path/to/your/file.php

<?php
$output = file_get_contents('http://host/path/another.php?param=value ');
echo $output;
?>

I think this is what you are looking for

<?php include ('Scripts/Php/connection.php');
//The connection.php script is executed inside the current file ?>

The script file can also be in a .txt format, it should still work, it does for me

e.g.

<?php include ('Scripts/Php/connection.txt');
//The connection.txt script is executed inside the current file ?>

I prefer to use

require_once('phpfile.php');

lots of options out there for you. and a good way to keep things clean.


The OP refined his question to how a php script is called from a script. The php statement 'require' is good for dependancy as the script will stop if required script is not found.

#!/usr/bin/php
<?
require '/relative/path/to/someotherscript.php';

/* The above script runs as though executed from within this one. */

printf ("Hello world!\n");

?>