code feedback

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anoveskey
Forum Newbie
Posts: 9
Joined: Sun Sep 26, 2010 7:16 pm

code feedback

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: code feedback

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
emelianenko
Forum Commoner
Posts: 35
Joined: Thu Sep 09, 2010 11:49 am

Re: code feedback

Post 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."; }
?>




Post Reply