Archived talk

Difference between revisions of "Creating a file uploader in your component"

From Joomla! Documentation

(5 intermediate revisions by 3 users not shown)
Line 42: Line 42:
 
Gopi
 
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.
 +
 +
06/03/2012
 +
 +
Edit to take this into account - Hopefully resolved!!
  
 
== Changed PHP script in Part 5 ==
 
== Changed PHP script in Part 5 ==
Line 117: Line 129:
  
 
Where the code is part 5?
 
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:
 +
 +
<?php
 +
 +
// Restore session that existed prior to flash uplosd
 +
 +
$sn = session_name();<br/>
 +
if (isset($_COOKIE[$sn]) && isset($_POST[$sn])) {<br/>
 +
$_COOKIE[$sn] = $_POST[$sn];<br/>
 +
session_id($_POST[$sn]);<br/>
 +
}<br/>
 +
 +
chdir('../..');<br/>
 +
include('index.php');
 +
 +
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

Revision as of 19:34, 5 March 2012

Hi

Implementing the SWFUpload Application demo for my component. The joomla document explains only the simple demo (http://docs.joomla.org/Creating_a_file_uploader_in_your_component )

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"]); }

session_start();

$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[edit]

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.

06/03/2012

Edit to take this into account - Hopefully resolved!!

Changed PHP script in Part 5[edit]

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.

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');

$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;

	default:
}

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 ?????? ",

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


Where the code is part 5?

Suggestion for Part 6[edit]

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:

<?php

// Restore session that existed prior to flash uplosd

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

chdir('../..');
include('index.php');

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