Archive

Archive for the ‘PHP’ Category

Flex: Multiple File Uploader

October 1st, 2009 13 comments

Flex Multi UploaderUploading is such a common task and can often be a little painful so I decided to write a small library in Flex which simplifies the task. I have released it under GPL license so feel free to use and modify it according to the conditions :)

The Flex client side uploader supports multiple file upload with queuing and multiple threads, meaning more than one upload can be active at a time. It is also written in component style so creating an instance of the uploader is very simple and can be achieved in only a few lines of code (see the example below). It also supports posted additional data with the file upload, handy if you need to pass instructions to the back-end upload script.

The back-end script is not included in the library but could easily be implemented in any language. I have however supplied a PHP example below of how to implement the back-end script to support the Flex uploader (see the example below).

One final note, this code is not polished, it’s definitely Alpha…but still very usable. If you have any issues or questions please let me know, cheers.

Simple example implementation of uploader library

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				xmlns:cc="FileUploader.Renderers.*" 
				layout="absolute"
			    creationComplete="{init()}"
			    width="1018"
			    height="455"
				>
	<mx:Script>
		<![CDATA[
			
			private function init():void{
				fileUploader.addPostData('mydata','1234567890');
			}
			
		]]>
	</mx:Script>
				
	<cc:FileUploadComp id="fileUploader"
					   x="0"
					   top="8"
					   left="8"
					   width="1000"
					   maxThreadsEnabled="true"
					   uploadUrl="http://a.b.c.d/MyUploaderScript.php"
					   />
</mx:Application>


PHP backend example implementation of uploader code

<?php
	$tmpFileName = isset($_FILES['Filedata']['tmp_name']) ? $_FILES['Filedata']['tmp_name'] : '';
	$orgFileName = isset($_FILES['Filedata']['name']) ? $_FILES['Filedata']['name'] : '';
	$mydata = isset($_REQUEST['mydata']) ? $_REQUEST['mydata'] : '';
	
	error_log('tmpfile: ' . $tmpFileName);
	error_log('filename: ' . $orgFileName);
	error_log('mydata: ' . $mydata);	

	if (true){
		print 'ok'; 
	}
	else{
		header('Status: 500 ' . $resultAry[1]);
		header('HTTP/1.1 500 ' . $resultAry[1]);
		
		$outputStr = '<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">' .
				 '<html><head>' .
				 '<title>500 ' . $resultAry[1] . '</title>' .
				 '</head><body>' .
				 '<h1>500 ' . $resultAry[1] . '</h1>' . 
				 '<hr>' .
				 $_SERVER['SERVER_SIGNATURE'] .
				 '</body></html>';
		print $outputStr;
	}
?>

Also here’s and example of a Flash crossdomain.xml (this one is reasonably open!) which will be needed in the root of your site…otherwise flex will complain.

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
	<allow-access-from domain="*" to-ports="*"/>
	<site-control permitted-cross-domain-policies="all"/>
</cross-domain-policy>

Demo Flex Uploader

DOWNLOAD (version 1.0a)

WordPress: Custom Post Order

September 25th, 2009 No comments

I recently had a requirement for custom ordering on WordPress categories and thought that I would share how I did it.

All that is needed is this piece of code which essentially modifies the SQL statement being generated. Note that the argument assigned to orderby is the column name, hence it is case sensitive (I found out that ID breaks the naming conversion on the post table);

<?php 
   if (is_category(array($myCatId))){
      query_posts($query_string . "&orderby=ID&order=DESC");
   } 
?>

Code needs to be placed before this line

<?php if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?>
Categories: Code, General, PHP, Wordpress Tags: ,

WordPress Adsense – Ad Targeting Redirect

August 20th, 2009 4 comments

When I was a Adsense n00b I made the mistake of adding adding a witty statement in my tagline in wordpress which confused adsense in terms of the content of my site, despite all the search engine optimization I had done . The symptom was: only the main index of my site had ads targeted solely on the wordpress “tagline” (the strip of text under the main title), all other pages on my site were targeted correctly. I thought this would be a matter of simply changing the tagline…but no, google doesn’t forget! I wont go into detail about the type of ads that were being targeted, lets just say they weren’t ideal.

A simple effective work around is a header redirect (permalink friendly) on the main site index which can be prefixed to the wordpress index.php in your wordpress top level directory.

<?php

// set $redirectHome = false to disable the redirect

$redirectHome = true;
if ($redirectHome){
        // Redirects index page
        $uriStr = $_SERVER['REQUEST_URI'];
        if (($uriStr == '/') || ($uriStr == '')){
                header('Location: /?index',true,301);
                die();
        }
        // Makes sure pages off the main index are not renamed 
        $matchAry = array();
        if (preg_match('/(\/page\/\d+\/)\?index/',$uriStr,$matchAry)){
                header('Location: ' . $matchAry[1],true,301);
                die();
        }
}

/**
 * Original WordPress index.php goes here
 */

This works because Google thinks “/?index” is a new page (it’s not though, it’s the main index with a query string attached) and the media bot crawls it and targets it accordingly as a new page. And as long as you’ve made your amendments to your previously incorrect content, in my case the “tagline” then this time around your ads should be accurately targeted…hopefully :)

The curious thing is, it seems no matter what only my original main index is targeted incorrectly still months later. I’ve seen other posts relating to incorrect ad targeting on the main index only. It seems like Google’s media crawler doesn’t like something about WordPress, I would like to hear from others that have experienced this.

Hello World Example

August 11th, 2009 No comments

No site would be complete without the textbook hello world example…so here it is 😀

<?php

/**
 * Hello World Example
 */

function foo($bar){
     print $bar;
}

$bar = 'Hello World';
foo($bar);

?>
Categories: PHP Tags: