Explain me the meaning of this PHP code.

Discussion in 'Programming' started by nura235, Jan 27, 2010.

  1. #1
    Hi,

    as i m not the programmer, i need a lit bit help of yours.

    please explain me the 2 piece of code below.
    // Directly count file sizes
    $handle = opendir ( "../files" );
    $totsiz = 0;
    $todaysize = 0;
    while ( $filee = readdir ( $handle ) ) {
    	if ($filee == '.' || $filee == '..' || is_dir($filee) || !file_exists(dirname(__FILE__)."/files/$filee")) continue;
    	$timey = filemtime( dirname(__FILE__)."/files/$filee" );
    	$siz = filesize ( dirname(__FILE__)."/files/$filee" );
    	if (time() - $timey < 86400) {
    		$todaysize += $siz;
    	}
    	
    	$filelist [$filee] = $siz;
    	$delist [$timey] = $filee;
    	$totsiz = $siz + $totsiz;
    }
    closedir ($handle);
    $origtot_size = $totsiz;
    $origmax_space = $maximum_space_usage_in_mb;
    $tot_size = byteConvert($totsiz);
    $maximum_space_usage_in_mb = byteConvert($maximum_space_usage_in_mb);
    
    Code (markup):
    And

    $REMOTE_ADDR = $_SERVER ['REMOTE_ADDR'];
    
    $res = executeQuery('SELECT * FROM users WHERE ip="'.$REMOTE_ADDR.'"');
    if ($res->numRows() == 0) {
    	$res = executeQuery('INSERT INTO users (`ip`,`downloads`,`vip`) VALUES ("'.$REMOTE_ADDR.'",0,0)');
    	$res = executeQuery('SELECT * FROM users WHERE ip="'.$REMOTE_ADDR.'"');
    }
    $row = $res->fetchRow();
    if ($row['vip'] == 1) $vip = 1;
    //$res =executeQuery('SELECT * FROM files WHERE ip="'.$REMOTE_ADDR.'"');
    //$heute = $res->numRows();
    $res = executeQuery('DELETE FROM downloads WHERE time<'.(time()-3600));
    $res = executeQuery('SELECT * FROM downloads WHERE ip="'.$REMOTE_ADDR.'"');
    $heute = $res->numRows();
    
    Code (markup):
    I want to increase the time how can i do that.
     
    nura235, Jan 27, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    First code snippet calculates size of files in a directory.
    Second one checks last 1 hour user downloads and sets whether user is vip or not.

    Seems like a file hosting script code snippet..
     
    mastermunj, Jan 27, 2010 IP
  3. TimothyJohn

    TimothyJohn Member

    Messages:
    786
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Ok from what mastermunj just said and what you have said i gather you want to increase the time that is current 24 hours which would be this part here
    
    if (time() - $timey < 86400) {
    		$todaysize += $siz;
                                            }
    
    Code (markup):
    86400 is the amount of seconds in a day so if you want to increase the time i suggest you increase this :)
    $res = executeQuery('DELETE FROM downloads WHERE time<'.(time()-3600));
    Code (markup):
    Or if you want to increase the last hour user downloads then you should increase this :) because 3600 is the seconds in an hour if im not mistaken.
    If i am completely wrong feel free to say so or +rep haha Good luck to you :)
     
    TimothyJohn, Jan 27, 2010 IP
  4. nura235

    nura235 Well-Known Member

    Messages:
    529
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    110
    #4
    thanks for the above poster.

    just look at this
    private function SaveInstance() {
    		// The file will be a variable in the future
    		$DownloadInstance = serialize($this->DownloadInstance);
    		$file = fopen('../instance.lst','w');
    		fwrite($file,$DownloadInstance);
    		fclose($file);
    	}
    	
    	private function PurgeInstance() {
    		/*foreach ($this->DownloadInstance as $id => $Instance) {
    			if (time() - $Instance['Created'] > 3600 * 5 ) {
    				unset($this->DownloadInstance[$id]);
    			}
    		}
    		$this->SaveInstance();*/
    		$vhandle = opendir ( "../instances" );
    		while ( $filename = readdir ( $vhandle ) ) {
    			if ($filename != '.' && $filename != '..' && !is_dir($filename)) {
    				$atime = fileatime("../instances/".$filename);
    				if (time() - $atime > 3600) {
    					unlink("../instances/".$filename);
    				}
    			}
    		}
    	}
    	
    	public function __deconstruct() {
    		//$this->SaveInstance();
    	}
    }
    
    Code (markup):
    what this 3600*5
    and if (time() - $atime > 3600) {

    do here
     
    nura235, Jan 28, 2010 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    if (time() - $Instance['Created'] > 3600 * 5 ) <== This checks whether 5 hours are passed to creation of this instance of file. If so, unset the download instance id.

    if (time() - $atime > 3600) <== checks that if tile was created an hour back, if so, delete the file.
     
    mastermunj, Jan 28, 2010 IP