Forums / Developer / Custom module - how to upload files?

Custom module - how to upload files?

Author Message

Roy Bøhmer

Tuesday 21 December 2004 3:00:11 am

Hi!
I'm making a module that will read a uploaded tab-separated textfile and store it in a db-table. I thought I'd understood the eZHTTPFile, but I can't make it work.

The template-form has

<input type="file" name="FileToImport">

Here is some of my modulecode:

<?php
include_once('kernel/common/template.php');
include_once('lib/ezutils/classes/ezhttpfile.php');

$tpl =& templateInit();
$module =& $Params['Module'];

$debug='';

if ( $module->isCurrentAction( 'store' ) )
{
	$debug = 'Action=store';
	if ( eZHTTPFile::canFetch( 'FileToImport' ) )
      {
		$debug='canFetch=1';
		$file =& eZHTTPFile::fetch( 'FileToImport' );
		if ( $file )
            {
			$debug='Fetched!';
		}
	}
}

$tpl->setVariable('debug', $debug);

// Build module result array
$Result = array();
$Result['content'] = $tpl->fetch("design:dailyword/storefile.tpl");
$Result['path'] = array( array( 'url' => '/dailyword/import',
                                'text' => "Import to database") );
?>

This code result in $debug being set to 'Action=store'. I.e. it cant fetch the file.
Any ideas?

Roy

Roy Bøhmer

Tuesday 21 December 2004 3:12:23 am

Just some more info:

$_FILES['FileToImport'] result in a warning: Undefined index.

Eirik Alfstad Johansen

Tuesday 21 December 2004 3:34:35 am

Hi Roy,

Here's what I use with success in my own custom module:

	if(is_uploaded_file($_FILES['filename']['tmp_name']))
	{
		echo "File uploaded successfully.";
	}

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Roy Bøhmer

Tuesday 21 December 2004 3:55:32 am

Thanks for your quick reply!

I found the error: The form-tag in the template were missing enctype="multipart/form-data". (arrgh..)

I still appreciating you response, Eirik!

Roy