Sunday, June 7, 2009

Rename file if exists in PHP

function rename_if_exists($dir, $filename) {
    $ext = strrchr($filename, '.');
    $prefix = substr($filename, 0, -strlen($ext));
    $i = 0;
    while(file_exists($dir . $filename)) { // If file exists, add a number to it.
        $filename = $prefix . ++$i . $ext;
    }

    return $filename;
}

Download youtube video’s with PHP

<?
//example string for the function
get_youtube_download_link("http://www.youtube.com/watch?v=8yOa-7fHNwo&feature=rec-HM-r2");

function str_between($string, $start, $end){ $string = " ".$string;
$ini = strpos($string,$start); if ($ini == 0) return ""; $ini +=
strlen($start); $len = strpos($string,$end,$ini) - $ini; return
substr($string,$ini,$len); }
function get_youtube_download_link($url){
if( $url !== "")
{
$youtube_link = $url;
//$url = get_post_meta($post->ID, 'url', true);
if ( file_get_contents($youtube_link) !== "")
{
$youtube_page = file_get_contents($youtube_link);
$v_id = str_between($youtube_page, "&video_id=", "&");
$t_id = str_between($youtube_page, "&t=", "&");
$flv_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id";
$hq_flv_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=6";
$mp4_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=18";
$threegp_link = "http://www.youtube.com/get_video?video_id=$v_id&t=$t_id&fmt=17";
echo "\t\tDownload (right-click &gt; save as)&#58;\n\t\t";
echo "<a href=\"$flv_link\">FLV</a>\n\t\t";
echo "<a href=\"$hq_flv_link\">HQ FLV (if available)</a>\n\t\t";
echo "<a href=\"$mp4_link\">MP4</a>\n\t\t";
echo "<a href=\"$threegp_link\">3GP</a><br><br>\n";
}
}
}
?>

Show the last x number of posts on Wordpress in PHP

This code shows the last x-number of posts (15 in this example) in your wordpress blog. Make sure you put in the proper directory in the ‘require()’  string. This code can be on a completely seperate page, fully outside of wordpress

<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./wordpress/wp-blog-header.php');
query_posts('showposts=15');
?>

<ul>
<?php while (have_posts()): the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

Copy an image from a remote URL to a local file

 

This code copies the remote image to your local server. it is pretty basic as it does not check if the file already exists. For that, use the rename if exist function.

<?
//original image
$img = "http://www.images.com/anyimage.jpg";
//isolate the filename from the URL
$fname= basename($img); 
//directory to copy to (must be CHMOD to 777)
$copydir = "/var/www/upload/";   // change as required
$data = file_get_contents($img);
$file = fopen($copydir . $fname, "w+");
fputs($file, $data);
fclose($file);
?>

Other possibility:


// example usage: download_file('whatever.jpg');
function download_file($filename, $mimetype = 'application/octet-stream')
{
  if (!file_exists($filename) || !is_readable($filename)) return false;
  $base = basename($filename);
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Content-Disposition: attachment; filename=$base");
  header("Content-Length: " . filesize($filename));
  header("Content-Type: $mimetype");
  readfile($filename);
  exit();
}

-------------------------------------

A third possibility:

<?php
$handle = fopen(“http://content8.flixster.com/photo/10/89/19/10891930_tml.jpg”, "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?>

De $contents can then be saved

 

Or even simpler:

<?
getfile('http://content8.flixster.com/photo/10/89/19/10891930_tml.jpg','upload' );

// $url is the url of the file
// $dir is the directory to save the file to relative to the current working directory
function getfile($url, $dir){
file_put_contents($dir.substr($url,strrpos($url,'/'),strlen($url)), file_get_contents($url));
}
?>

Obviously this works for non-image files as well