Tag Archives: php

Clear old cache files script

File: clear-cache.php

<?php
$files = glob(‘*’);
$now = time();
$expire = 60 * 60 * 24 * 7;//7 days
header(‘Content-Type: text/plain’);
$count = 1;
$myName = basename(__FILE__);//clear-cache.php
foreach ($files as $file) {
    if (basename($file) == $myName) continue;
    $cached_time = filemtime($file);
    if ($now – $cached_time > $expire) {
        $ok = @unlink($file);
        if (!empty($ok)) echo “$count. Deleted $file\n”;
        $count++;
    }
}

PHP script download Youtube clip

<?php
    error_reporting(0);
    /*
    @author: huyphungvan
    @date: 10:30 AM 8/23/2009
    @customer: diendantiengiang.com
    */
    function downloadFLV($vid = ‘5-d2eZrDcns’) {
        if (empty($vid)) return false;
        $url = ‘http://www.youtube.com/get_video_info?&video_id=’ . $vid;
        $source = file_get_contents($url);
        $source = urldecode(urldecode($source));
        $head = strpos($source, “|”);
        if ($head === false) return false;
        $tail = strpos($source, “|”, $head + 1);
        if ($tail === false) return false;
        $flv = substr($source, $head + 1, $tail – $head);
        $pos = strrpos($flv, “,”);
        $flv = substr($flv, 0, $pos);
        // echo $source;
        // echo “<hr />”;
        return $flv;
    }
    // URL: /download.php/9_AoAgrgZ70
    
    $vid = $_SERVER[“PATH_INFO”];
    $vid = substr($vid, 1);
    
    $flv = downloadFLV($vid);
    if (!empty($flv)) header(“Location: $flv”);
    else {
        header(“HTTP/1.0 404 Not Found”);
        echo ‘File not found’;
    }
?>

PHP Memcache and APC extensions

What is Memcache ?

Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

What is APC ?

APC Cache (Eaccelerator and other similar caches) is Fast but it is not distributed so you’re wasting cache and reducing possible hit rate by caching things locally if you have many web servers. MemcacheD is relatively slow but distributed and so you do not waste memory by caching same item in a few places, it is also faster to warmup as you need only one access to bring item into the cache, not access for each of web servers.

The good thing however is you do not have to select one or another, you can use both at the same time. APC will be great for caching small but frequently accessed things which are not taking too much memory. For example if you store list of states in the database you can cache it this way. For NNSEEK we can use it to cache list of languages, top groups and much of semi-static modules shown on group directory pages. Memcached is good for caching things which take large amount of space combined and which you only need to fetch few per page. For example search results may be good candidate (assuming we want to cache them and want to cache them in memory).

– Linux: memcache.so, apc.so (apt-get install or pecl)
– Windows: php_memcache.dll, php_apc.dll (google to get)

MVC – Model View Controller & PHP

The browser makes a request to web server by request URL.

The web server (Apache, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in configuration file. The web server then uses the dispatcher to create a new controller, call the action and pass the parameters.

Controllers do the work of parsing user requests, data submissions, cookies, sessions and the “browser stuff”.

Models are PHP classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting.

Views are what the user sees: HTML, CSS, XML, Javascript, JSON. They’re the sales rep putting up flyers and collecting surveys, at the manager’s direction. Views are merely puppets reading what the controller gives them. They don’t know what happens in the back room.

The controller returns the response body (HTML, XML, etc.) & metadata (caching headers, redirects) to the server. The server combines the raw data into a proper HTTP response and sends it to the user.