I have the following code stored in a file called index.php.
<?php
file_put_contents(date('Y-m-d-H-i-s', time()) . '.txt', '123');
sleep(1);
header("Location: ./index.php", true, 301);
?>
In a browser, it creates a file every second, but when I run it in php CLI (php -f index.php) it just creates one file.
How can I use redirect in the php CLI?
The command line interface (CLI) cannot redirect because it is not a browser. What you need is a loop. Try looking up a ‘for loop’ or a ‘while loop’. A ‘for loop’ repeats code a number of times and a ‘while loop’ repeats code until a condition is met.
For example the following code will loop 10 times: