Creating a file uploader in your component

From Joomla! Documentation

Revision as of 09:42, 4 March 2012 by Wilsonge (Talk | contribs)


Implementing the SWFUpload Application demo for my component. The joomla document explains only the simple demo ( )

what are the changes i have to do from the joomla doc. How i rewrite the thumbnail picture function for joomla 1.5

I tried so many time but not works out

<?php // This script accepts an ID and looks in the user's session for stored thumbnail data. // It then streams the data to the browser as an image

// Work around the Flash Player Cookie Bug if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); }


$image_id = isset($_GET["id"]) ? $_GET["id"] : false;

if ($image_id === false) { header("HTTP/1.1 500 Internal Server Error"); echo "No ID"; exit(0); }

if (!is_array($_SESSION["file_info"]) || !isset($_SESSION["file_info"][$image_id])) { header("HTTP/1.1 404 Not found"); exit(0); }

header("Content-type: image/jpeg") ; header("Content-Length: ".strlen($_SESSION["file_info"][$image_id])); echo $_SESSION["file_info"][$image_id]; exit(0); ?>

Pls help

Regards Gopi

Deprecated function used in the example

The ereg_replace function used is deprecated in PHP 5.3 so the example doesn't work:

//lose any special characters in the filename $fileName = ereg_replace("[^A-Za-z0-9.]", "-", $fileName);

Needs to be reviewed.

Changed PHP script in Part 5

I made some changes to my own work, before using it. Some tests to use JFile versions and the code is shorter. Return value true on success or else false if there was an error.

$fieldName  = 'file_upload';
$fileTemp   = $_FILES[$fieldName]['tmp_name'];
$fileName   = JFile::makeSafe( $_FILES[$fieldName]['name'] );
$fileExt    = strtolower( JFile::getExt( $fileName ) );
$imageinfo  = getimagesize( $fileTemp );
$mimetypes  = array( 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif' );
$uploadPath = JPATH_COMPONENT . DS . 'images' . DS . $fileName;
switch ( $_FILES[$fieldName]['error'] ) {
        case 1:
                echo JText::_( 'FILE LARGER THAN PHP INI ALLOW' );
                return false;
        case 2:
                echo JText::_( 'FILE LARGER THAN HTML FORM ALLOW' );
                return false;
        case 3:
                echo JText::_( 'ERROR PARTIAL UPLOAD' );
                return false;
        case 4:
                echo JText::_( 'ERROR NO FILE' );
                return false;
if ( $_FILES[$fieldName]['size'] > 2000000 ) {
        echo JText::_( 'FILE BIGGER THAN 2MB' );
if ( !in_array( $fileExt, explode(',', 'jpeg,jpg,png,gif') ) ) {
        echo JText::_( 'INVALID EXTENSION' );
        return false;
} else if ( !in_array( $imageinfo['mime'], $mimetypes ) ) {
        echo JText::_( 'INVALID FILE TYPE' );
        return false;
} else if ( !is_int( $imageinfo[0] ) || !is_int( $imageinfo[0] ) ) {
        echo JText::_( 'INVALID FILE SIZE' );
        return false;
if( !JFile::upload( $fileTemp, $uploadPath ) ) {
        echo JText::_( 'ERROR MOVING FILE' );
        return false;
return true;

Regards hasse_bjork

Helpme Please!!

Part 3: Adding The Head Javascript

upload_url: "index.php ?????? ",

               "option" : "com_mycomponent",
               "controller" : "?????",
               "task" : "????",

Where the code is part 5?

Suggestion for Part 6

Instead of editing a core file like libraries\joomla\session\session.php I found an alternative. In your component, create a file that looks like this:


// Restore session that existed prior to flash uplosd

$sn = session_name();
if (isset($_COOKIE[$sn]) && isset($_POST[$sn])) {
$_COOKIE[$sn] = $_POST[$sn];


Then in part 3, set upload_url equal to the path to the file you just created. It works for me, at least in localhost. What do you think?

regards, John