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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 19 2025 23:50:42
Script start
Timing: Jan 19 2025 23:50:42
Module start 'layout'
Timing: Jan 19 2025 23:50:42
Module start 'content'
Timing: Jan 19 2025 23:50:43
Module end 'content'
Timing: Jan 19 2025 23:50:43
Script end

Main resources:

Total runtime0.9439 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0047 589.1641152.6250
Module start 'layout' 0.00470.0025 741.789139.4609
Module start 'content' 0.00720.9353 781.2500544.1719
Module end 'content' 0.94240.0014 1,325.421912.1563
Script end 0.9438  1,337.5781 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.3240160.0002
Check MTime0.00130.1390160.0001
Mysql Total
Database connection0.00050.056010.0005
Mysqli_queries0.886493.9088600.0148
Looping result0.00050.0525580.0000
Template Total0.906996.120.4535
Template load0.00170.179920.0008
Template processing0.905295.902920.4526
Template load and register function0.00020.016210.0002
states
state_id_array0.00070.072410.0007
state_identifier_array0.00110.119020.0006
Override
Cache load0.00140.1501250.0001
Sytem overhead
Fetch class attribute can translate value0.00060.063020.0003
Fetch class attribute name0.00090.099650.0002
XML
Image XML parsing0.00060.067920.0003
class_abstraction
Instantiating content class attribute0.00000.001150.0000
General
dbfile0.00080.0807160.0000
String conversion0.00000.001140.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 18
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs