0

I have a php file that points to a url like so:

<?php
header('Content-Type: text/plain');
echo file_get_contents($_GET['url']);

The thing I would like my php file to do in addition to pointing to the URL is write a file to the server at a specific time each day -- let's say 6:00pm (18:00).

From my initial research, the write file part seems straight forward

file_put_contents('todaysDateData.txt', $_GET['url']);

However, figuring out how to call file_put_contents each day at a specific time is throwing me for a loop.

Question: Is it possible to write a file in the manner described above at a specific time each day in native php? Ideally I would like to name the file with today's date and "Data" suffix (i.e. Mar-29-2018.txt).

Edit

Going from the suggestions below, I have researched cron: I think the syntax for my use case is:

0 18 * * * path/to/my/command.sh

I think the date syntax is correct, but I'm still confused as to how cron is used with my php file. Should I change the sh file extension to php since I'm using file_put_contents? Or is this cron date thing going in my php file itself and I have to make a .sh (whatever that is?)

Arash Howaida
  • 2,235
  • 2
  • 13
  • 34
  • 2
    You're looking for cron – pavel Mar 29 '18 at 02:48
  • Or if you're on Windows: https://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron – Mike Mar 29 '18 at 02:49
  • CRON can run any file you just give it the command you would use in the shell to execute e.g. `php file.php` to have PHP execute a file named `file.php`. – chris85 Mar 29 '18 at 03:52
  • @chris85 Just to clarify, `php file.php` there is a space between `php` and the file name right? The comment had the line over two lines so I couldn't quite tell for sure. If I understand it correctly, it's like the first item is the code language. – Arash Howaida Mar 29 '18 at 06:42
  • What I did was enter this at the shell: `php path/to/my/file.php` I hope that was right, I guess I will find out at 6:00pm... – Arash Howaida Mar 29 '18 at 07:18
  • Yup that should be right. That assumes `php` runs PHP for you on the command line. If `php path/to/my/file.php` works for you in the shell it should work from the CRON as well, assuming you are using the same linux user. – chris85 Mar 29 '18 at 12:16

1 Answers1

1

You can check timetamp and file name.

<?php

$todayHour = 18;
$todayMinute = 0;
$timeCreate = mktime($todayHour, $todayMinute, 0, date('m'), date('d'), date('Y'));
$todayFilename = 'data-' . date('Y-m-d') . '.txt';

if (time() >= $timeCreate && !file_exists($todayFilename)){
    // Code input content file here
    file_put_contents($todayFilename, $_GET['url']);
}
Mr Ken
  • 319
  • 1
  • 17