How to cache PHP objects to disk with CakePHP
CakePHP has a view cache (similar to Rails) that can be used to cache objects. The following snippet shows a CakePHP action that uses the serialize and unserialize functions to cache a tag cloud—an array containing tags in this case—to disk, and then read it back.
Note that we assign a TTL of 1 hours to the tag cloud, so if it’s more than one hour old it will be refreshed from the database.
1 function index() 2 { 3 $maximum = 100; 4 $cache_key = "tag_cloud_$maximum"; 5 $tag_cloud = cache($cache_key, null, '+1 hours'); 6 7 if(empty($tag_cloud)) 8 { 9 $tag_cloud = Tag::generate_cloud($maximum); 10 cache($cache_key, serialize($tag_cloud)); 11 } 12 else 13 { 14 $tag_cloud = unserialize($tag_cloud); 15 } 16 17 return $tag_cloud; 18 }
The cache function is defined in $APP_ROOT/cake/basics.php, which is where you should look if you want to know more about how the caching works…
How to format number of seconds as duration with PHP
A very sophisticated algorithm that will display the length of, for example, a video as 12:01:30.
1 function duration($seconds_count) 2 { 3 $delimiter = ':'; 4 $seconds = $seconds_count % 60; 5 $minutes = floor($seconds_count/60); 6 $hours = floor($seconds_count/3600); 7 8 $seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT); 9 $minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT).$delimiter; 10 11 if($hours > 0) 12 { 13 $hours = str_pad($hours, 2, "0", STR_PAD_LEFT).$delimiter; 14 } 15 else 16 { 17 $hours = ''; 18 } 19 20 return "$hours$minutes$seconds"; 21 }
How to upload a file with PHP and lib_curl
This is an example of how to upload a file from PHP to another server that may or may not be running PHP .
Note that using Pear’s HTTP _Request package is probably easier.
1 <?php 2 require_once('Var_Dump.php'); 3 Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4)); 4 5 $url = 'www.aktagon.com/upload'; 6 $file = '/tmp/file_to_upload'; 7 $post_data = array(); 8 9 $post_data['username'] = ''; 10 $post_data['password'] = ''; 11 $post_data['file'] = "@$file"; 12 13 $ch = curl_init(); 14 15 curl_setopt($ch, CURLOPT_URL, $url); 16 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 17 # 'Cookie: xxxx', 18 # 'Content-Type: xxxx' 19 )); 20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 21 curl_setopt($ch, CURLOPT_POST, true); 22 #curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 23 24 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 25 26 $response = curl_exec($ch); 27 $info = curl_getinfo($ch); 28 $http_code = $info['http_code']; 29 $speed = round($info['speed_upload'] / 1024); 30 $size = round($info['size_upload'] / 1024); 31 32 if($http_code != 200) 33 { 34 echo "Upload failed with HTTP code $http_code. Uploaded $size kilobytes with $speed kilobytes/s.<br/>"; 35 echo "<h1>Response</h1><pre>$response</pre><h1>Debug information</h1>"; 36 37 Var_Dump::display($info); 38 } 39 else 40 { 41 echo "Uploaded successful. Uploaded $size kilobytes with $speed kilobytes / s.<br/>"; 42 echo "<h1>Response</h1><pre>$response</pre>"; 43 } 44 curl_close($ch); 45 ?> 46
You can remove the Var_Dump code (Pear module), if you haven’t installed it, or don’t need it.
How to print all HTTP headers with PHP
1 <ul> 2 <?php 3 foreach($_SERVER as $h=>$v) 4 if(ereg('HTTP_(.+)',$h,$hp)) 5 echo "<li>$h = $v</li>\n"; 6 header('Content-type: text/html'); 7 ?> 8 </ul>
How to add logging to CakePHP applications
Note that Pear’s Logging package is a lot more flexible, so I recommend you use that instead of CakePHP’s built-in logging.
Use this code to add a debug message to the CakePHP debug log:
1 $this->log("Upload action accessed", LOG_DEBUG);
Note that using something other than LOG _DEBUG will log the message as an error.
Run the code once and you should be able to see the message in $CAKE_APP/tmp/logs/debug.log.