Page 1 of 1

code feedback

Posted: Sun May 22, 2011 1:33 am
by anoveskey
Hi all!

Not really much in the way of a question at this point, more of an inquiry. On my site, on June 1st, I will have the index.php page loaded as this chunk of code. My intention is to give viewers an opportunity to download a file for 24 hrs. Once the 24 hrs is up the script should unlink the file in question as well as itself. I guess my question (if any) is do I understand the unlink function correctly? will this in essence delete the opening page and the file or will people still be able to access the opening page and file if they type in the URL to the resource? If I have to delete it manually than I will, I would just rather automate as much of the process as possible:

Code: Select all

<a href="http://www.mywebsite.com/fileToDownload.zip"><img src="http://www.mywebsite.com/images/picForFile.jpg" width="450" alt="Pic For File" /></a><br />
<?php
countdown(6,1,24,0,0);

function countdown($month, $day, $hour, $minute, $second)
{
  $the_countdown_date = mktime($hour, $minute, $second, $month, $day);

  $today = time();

  $difference = $the_countdown_date - $today;
  if ($difference < 0) $difference = 0;

  $days_left = floor($difference/60/60/24);
  $hours_left = floor(($difference - $days_left*60*60*24)/60/60);
  $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
  $seconds_left = floor((($difference - $days_left*60*60*24 - $hours_left*60*60 - $minutes_left*60)*60)/60);

  if ($the_countdown_date == $today) {
  	$myJunk = "fileToDownload.zip";
	$myOtherJunk = "index.php";
  	unlink($myJunk);
	unlink($myOtherJunk);
	}
  
  echo "For the next " .$days_left. " days " .$hours_left."h : ".$minutes_left."m : " .$seconds_left."s<br /> ";
  echo "download this file for free!<br />";
  echo '<a href="http://www.mywebsite.com/index2.php">Enter the site!</a>';
}
?>
So what do you guys think?

Re: code feedback

Posted: Sun May 22, 2011 7:46 am
by social_experiment
anoveskey wrote: I guess my question (if any) is do I understand the unlink function correctly? will this in essence delete the opening page and the file or will people still be able to access the opening page and file if they type in the URL to the resource?
It looks like you have the correct understanding of the function. However, this will only work if the script is accessed. If the 24 hours is done and the page is not accessed, the file will not be deleted. For something like that you will probably use a cron job (no idea how those work). If the file is deleted it shouldn't be accessable via url.

Re: code feedback

Posted: Sun May 22, 2011 9:01 am
by emelianenko
You need to close it first. Also, you may want to have a look at all the documentation of the unlink function on the php manual. You can copy and paste recursive algorithms to delete the directory where you can place your file.

Code: Select all


<?php
    chdir('../pics/');
    $do = unlink($fileToDel);
    if($do=="1"){
        echo "The file was deleted successfully.";
    } else { echo "There was an error trying to delete the file."; }
?>