Search:

Categories



QR code generation in PHP

This simple example uses the QR code library that can be located at http://phpqrcode.sourceforge.net

here is a very basic example, there are many examples available on their website showing the power of the library

 

<?php
include(‘libs/qrcodes/qrlib.php’);
// outputs image directly into browser, as PNG stream
QRcode::png(‘PHP QR Code example’, ‘filename.png’);
?>

recursive copying of files and folder

// copy recursive
function cpy($source, $dest){
if(is_dir($source)) {
$dir_handle=opendir($source);
while($file=readdir($dir_handle)){
if($file!=”.” && $file!=”..”){
if(is_dir($source.”/”.$file)){
if(!is_dir($dest.”/”.$file)){
mkdir($dest.”/”.$file);
}
cpy($source.”/”.$file, $dest.”/”.$file);
} else {
copy($source.”/”.$file, $dest.”/”.$file);
}
}
}
closedir($dir_handle);
} else {
copy($source, $dest);
}
}

Expand a short url

< ?php
$url = “http://shorturlhere.com”;
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,false);
$data = curl_exec($ch);
$pdata = http_parse_headers($data);
echo “Short URL: {$url}<br/>”;
echo “Original URL: {$pdata['Location']}”;

function http_parse_headers( $header )
{
$retVal = array();
$fields = explode(“\r\n”, preg_replace(‘/\x0D\x0A[\x09\x20]+/’, ‘ ‘, $header));
foreach( $fields as $field ) {
if( preg_match(‘/([^:]+): (.+)/m’, $field, $match) ) {
$match[1] = preg_replace(‘/(?< =^|[\x09\x20\x2D])./e’, ‘strtoupper(“”)’, strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
}
?>

mp3 to ogg conversion

<?php
$ffmpeg = exec(‘which ffmpeg’);
$ffmpegcmd = $ffmpeg.” -i test.mp3 -acodec libvorbis test.ogg”;
echo $ffmpegcmd;
exec($ffmpegcmd);
?>

File Upload php Script

<html>
<head>
<title>File Upload Script</title>
</head>
<body>
<div>
<?php
if ( isset( $_FILES['fupload'] ) ) {

print “name: “. $_FILES['fupload']['name'] .”<br />”;
print “size: “. $_FILES['fupload']['size'] .” bytes<br />”;
print “temp name: “.$_FILES['fupload']['tmp_name'] .”<br />”;
print “type: “. $_FILES['fupload']['type'] .”<br />”;
print “error: “. $_FILES['fupload']['error'] .”<br />”;

if ( $_FILES['fupload']['type'] == “image/gif” ) {

$source = $_FILES['fupload']['tmp_name'];
$target = “upload/”.$_FILES['fupload']['name'];
move_uploaded_file( $source, $target );// or die (“Couldn’t copy”);
$size = getImageSize( $target );

$imgstr = “<p><img width=\”$size[0]\” height=\”$size[1]\” “;
$imgstr .= “src=\”$target\” alt=\”uploaded image\” /></p>”;

print $imgstr;
}
}
?>
</div>
<form enctype=”multipart/form-data”
action=”<?php print $_SERVER['PHP_SELF']?>” method=”post”>
<p>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”102400″ />
<input type=”file” name=”fupload” /><br/>
<input type=”submit” value=”upload!” />
</p>
</form>
</body>
</html>

Site is down redirection

<?php
$host = ‘www.yoursitehere.com’;
$up = ping($host);
//site is up
if( $up )
{
header(‘Location: http://’.$host);
}

//site is down
else
{
header(‘Location: http://www.yourothersite.com’);
}
?>