using JStream with XMLHttpRequest (XHR)

105 views
Skip to first unread message

Adam Stephen Docherty

unread,
Aug 15, 2012, 1:21:22 AM8/15/12
to Joomla! General Development
Hello there I am saving files from a XMLHttpRequest (XHR) request and
I just can't get it to work with joomla's framework methods. It seems
that they do different things than their named counterparts in native
PHP. The following code works - commented out are the Joomla methods,
when added they throw errors.

$input = JFactory::getApplication()->input;

$id = $input->get( 'eventid', false, 'int' );

//$stream = JFactory::getStream();

$input = fopen("php://input", "r");
//$input = JFile::read("php://input");

$temp = tmpfile();

stream_copy_to_stream($input, $temp);
//$stream->copy($input, $temp);
fclose($input);

$pathinfo = pathinfo( $input->get( 'qqfile', false, 'string' ) );
$filename = $pathinfo['filename'];
$ext = @$pathinfo['extension']; // hide notices if extension
is empty

$uploadDirectory = dirname( __FILE__ ); //just for testing

$target = fopen( $uploadDirectory . $filename . '.' . $ext, "w" );
//$input = JFile::read("php://input");

fseek( $temp, 0, SEEK_SET );
//$stream->seek($temp, 0, SEEK_SET);

stream_copy_to_stream( $temp, $target );
//$stream->copy( $temp, $target );
fclose( $target );

any advice on this would be very much appreciated.

Kind Regards,

Adam

Adam Rifat

unread,
Aug 15, 2012, 4:27:19 AM8/15/12
to joomla-de...@googlegroups.com
Hi Adam,

I've used this recently and it works fine for me. I lifted a lot of code from com_media/controllers/file.php for the file (upload) checking etc but I placed it in a subcontroller of my component and it works fine. 

Did you include the file system lib at the top of the script?

// Import file lib for checking file types of files being uploaded
jimport('joomla.filesystem.file');

Cheers,

Adam
Reply all
Reply to author
Forward
0 new messages