How do you run a cron from a php script

I have a php script that is executed by a user and sets “fileone = 1” in the database.
After that script is executed, I would like to add a cron.php as a include.

I want the following php script to execute once automatically after 7 days has past


<?php
include "Dbcon.php";
$result = mysql_query("UPDATE myfile SET fileone='0' WHERE fileone='1'") 
or die(mysql_error());  
mysql_close();
?>

What I an example of a php script of what I need, but this example does not work.
Can someone help either make this script work or show me one that will?


#!usr/local/lib/php 
flag_file="/home/amour/public_html/phplogin/.cleanup_flag"
script_file="/home/amour/public_html/phplogin/cron.php"
offset_days=7
&lt;?php
if ( -z $flag_file ){

        time_last_run=`cat $flag_file`;
        time_today=`date +%s`;

        time_diff=(($time_today-$time_last_run));
        time_offset=(($offset_days*86400));

        if ( "$time_diff" -ge "$time_offset" ){
        
                php $script_file;
                rm $flag_file;
        }
}
?&gt;

You don’t need cron for that at all :slight_smile:

Just add a column of type DATETIME to the table, let’s call it lastDownload

Then, when someone downloads the file you perform the following UPDATE:


UPDATE
   my_table
SET
   fileone=1
 , lastDownload=NOW()

Now, to check if a file is currently “locked” (that’s essentially what you’re after) perform the following SELECT:


SELECT
   COUNT(*)
FROM
   my_table
WHERE
   file_id=<some_file_id>
      AND
   (
      fileone=0
         OR
      lastDownload < NOW() - INTERVAL '7' DAY
   )

If the count is 1 the file is either not downloaded, or downloaded more than 7 days ago. If the count is 0 the file is locked (downloaded no more than 7 days ago)

If you want to grab the locked info for all files in one go you could use the following SELECT:


SELECT
   some
 , fields
 , describing
 , the
 , file
 , fileone=0 OR lastDownload < NOW() - INTERVAL '7' DAY AS locked
FROM
   my_table

If locked is 1 the file can not be downloaded, if it’s 0 it can be downloaded.

Does that make sense? :slight_smile:

The download from scenario (2) will be step (1) so yes, it does update the lastDownload so no one else can download the file for the next seven days :slight_smile:

Actually step (4b) doesn’t really exist, it’s just a different way of writing step (1)

I am sorry I will give it a try, I thought when you said in post #8 that I would to set it my self, i would have to do that manually, I misunderstood. The is already being done in my upload php file

What happens in scenario 2 when the person downloads the file does it reset so no one can download the file again until it is uploaded or if 7 days pass

I keep failing to see why you need cron.
Let me walk you through it

(1) A user wants to download a file. If for that file fileone=0 or lastDownload is more than 7 days ago they can (note that only one of the conditions has to be true for the compound condition to be true)
(2) When they download the file fileone is set to 1 and lastDownload is set to NOW()

– at this point no other users can download the file because fileone is not 0 and lastDownload is not 7 days or more ago –

SCENARIO 1:
(3a) User edits the file and uploads it again
(4a) Application sets fileone to 0 and the file is available for download again (since step (1) will allow the download of a file if fileone is set to 0)

SCENARIO 2:
(3b) The user does nothing with the downloaded file for seven days or more – i.e. doesn’t re-upload it
(4b) If someone else wants to download the file after these seven days they can because step (1) allows the download of a file if lastDownload is more than seven days ago. It doesn’t matter that fileone is set to 1 at this point.

What about this solution don’t you think will work?

That is the reason I ask for the help on the php cron at the beginning of this post, I have to many user to go in and make that change changes.
I need to go back to the setup up of the cron php, if you can help

That would not work.

I have a file that users download and then make changes on it and upload it back up again.

When the person downloads the file it sets the fileone to “1” and that stop anyone from downloading unitil the file is upload and than it set the fileone to “0”, but just in case someone didn’t upload the file back, I want to set a limit on the time someone would have to wait to upload the file. So thought 7 days was good enough time to wait and the it would set the fileone to “0”

You need to set that yourself when the user uploads the file again.
If they don’t do that within 7 days the SELECT query will also report the file is available for download because the last download was more than 7 days ago.
Please read post #4 again, carefully :slight_smile:

Could you explain in a bit more detail what you’re trying to do?
It really sounds to me that you’re over complicating things.
For example, wouldn’t it be easier to also store in the database when the value was set to 1 and then run a cron script every day that updates all rows that have value 1 and were set 7 days ago or more to set the value back to 0?

i.e. something like


UPDATE
   my_table
SET
   fileone=0
WHERE
   fileone=1
      AND
   whenWasItSet <= CURDATE() - INTERVAL '7' DAY

Again will this change the fileone to “0”?
I all ready have a script that locks the download, I just need to have a way to change the fileone to “0” after seven days automatically, so it can be downloaded again

You should not look at the number of returned rows, but at the value of COUNT(*) of the single returned row. Thus:


<?php
include "Dbcon.php";
$query="select COUNT(*) AS count from uploadedfiles WHERE fileone='0' OR lastDownload1 < NOW() - INTERVAL '7' DAY";
$result=mysql_query($query);
$row=mysql_fetch_assoc($result);
if ($row['count'] == 1) {
mysql_query("update uploadedfiles set fileone=1, lastDownload1 = NOW()")or die(mysql_error());
?>
<center><a href="/download/Master 2000 thru 2005.xls"><b><u><font size = 4>Download Master 2000 thru 2005.xls file</b></u></font></a></center>
<?php

}
else
{
echo "Sorry someone has download this file you will have to wait until it has been upload again, check back later.";
}
mysql_close();
?>

:slight_smile:

Sorry let something out, when I run the script it goes to:

echo “Sorry someone has download this file you will have to wait until it has been upload again, check back later.”;

Which should not happen unless the fileone is set to “1” and there is a date in the filedownload field.

Here is my script, I don’t know if I have something wrong or not, but it does not record any data, the fileone=0 and there is nothing in the filedownload.

<?php
include “Dbcon.php”;
$query=“select COUNT(*) from uploadedfiles WHERE fileone=‘0’ OR lastDownload1 < NOW() - INTERVAL ‘7’ DAY”;
$result=mysql_query($query);
$num=mysql_num_rows($result);
if ($num == 0) {
mysql_query(“update uploadedfiles set fileone=1, lastDownload1 = NOW()”)or die(mysql_error());
?>
<center><a href=“/download/Master 2000 thru 2005.xls”><b><u><font size = 4>Download Master 2000 thru 2005.xls file</b></u></font></a></center>
<?php

}
else
{
echo “Sorry someone has download this file you will have to wait until it has been upload again, check back later.”;
}
mysql_close();
?>

Put will this set the fileone to “0” automatically so it can be download

Cool. How did you manage to do that?
Anyway, the scheme I outlined above will still work, since it checks for fileone=0 :slight_smile: