I’m using CodeIgniter in a project with the AJAX file uploader qqFileUploader. I wanted my CI controller to handle the upload work for the file uploader instead of a PHP script that lived outside the CI framework. To make this work made a few minor changes to the qq files and I made some changes in CI that force the uploader to always use XHR upload for now and not iframes. Just updated the code to work with iFrames and tested successfully on IE6 and IE8.

Here are the changes:

First, copy the qqfileuploader.php class file to your application/libraries folder. We’ll be loading this as a library in the controller. UPDATE: You’ll want to add a function to the qqFileUploader class in this file. The function will help get the filename for iframe-based uploads. Here is the new function:


public function getFilename() {
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'] . '.' . $pathinfo['extension'];
return $filename;
}

Second, make a minor change to the qqfileuploader.js javascript file so that it will pass the file information to CI using the CI-style URI instead of with query string parameters:

Find the qq.obj2url function. Comment out these two lines:

? encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj())
: encodeURIComponent(nextTemp) + '=' + encodeURIComponent(nextObj)

and in their place put:

? encodeURIComponent(nextObj())
: encodeURIComponent(nextObj)

This just passes the file name of the object to CI and skips the parameter= part. Next find this line:

prefix = (/\?/.test(temp)) ? (/\?$/.test(temp)) ? '' : '&' : '?';

and replace it with:

prefix = (/\?/.test(temp)) ? (/\?$/.test(temp)) ? '' : '/' : '/';

Lastly find:

uristrings.push(encodeURIComponent(temp) + '=' + encodeURIComponent(obj));

And replace it with:

uristrings.push(encodeURIComponent(obj));

Now for the CI controller function:


function uploadFile($filename = null) {
// This forces qqfileuploader to AJAX XHR mode

if (!isset($_FILES['qqfile'])) {
$_GET['qqfile'] = $filename;
}

$this->load->library('qqfileuploader');
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
$allowedExtensions = array('jpg','pdf','png','doc','docx','xls','xlsx','png');
// max file size in bytes
$sizeLimit = 2 * 1024 * 1024;

$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

$result = $uploader->handleUpload('/tmp/', true);

// Check if the filename is empty which it will be for iframe-based uploads
if (empty($filename)) {
$filename = $uploader->getFilename();
}

if ($result) {
// Get the mime type of the file
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, '/tmp/' . $filename);
$filedata = file_get_contents('/tmp/' . $filename);
// Do something with the file data, like save it to a BLOB or move it somewhere!
}

if ($result) {
// to pass data through iframe you will need to encode all html tags
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
}
else {
echo htmlspecialchars(json_encode(array("success" => false)), ENT_NOQUOTES);
}

}

Enjoy!