We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Why are my form variables not getting found?

Development and Programming

DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Using this upload script in a template (have tried PHP on both input / output):

<?
include($_SERVER['DOCUMENT_ROOT'] . '/upload/class.upload_27.php');

$dir_dest = $_SERVER['DOCUMENT_ROOT'] . "/otheruploads/";

$files = array();
    foreach ($_FILES['my_field'] as $k => $l) {
        foreach ($l as $i => $v) {
            if (!array_key_exists($i, $files))
                $files[$i] = array();
            $files[$i][$k] = $v;
        }
    }


foreach ($files as $file) {

        // we instanciate the class for each element of $file
        $handle = new Upload($file);

        // then we check if the file has been uploaded properly
        // in its *temporary* location in the server (often, it is /tmp)
        if ($handle->uploaded) {

            // now, we start the upload 'process'. That is, to copy the uploaded file
            // from its temporary location to the wanted location
            // It could be something like $handle->Process('/home/www/my_uploads/');
            $handle->Process($dir_dest);

            // we check if everything went OK
            if ($handle->processed) {
                // everything was fine !
                echo '<fieldset>';
                echo '  <legend>file uploaded with success</legend>';
                echo '  ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
                echo '  link to the file just uploaded: <a >file_dst_name . '">' . $handle->file_dst_name . '</a>';
                echo '</fieldset>';
            } else {
                // one error occured
                echo '<fieldset>';
                echo '  <legend>file not uploaded to the wanted location</legend>';
                echo '  Error: ' . $handle->error . '';
                echo '</fieldset>';
            }

        } else {
            // if we're here, the upload file failed for some reasons
            // i.e. the server didn't receive the file
            echo '<fieldset>';
            echo '  <legend>file not uploaded on the server</legend>';
            echo '  Error: ' . $handle->error . '';
            echo '</fieldset>';
        }
    }
?>

It works fine if I store it as a native PHP file, but as soon as I try accessing it via a template, I get the following errors:

Notice: Undefined index: my_field in /home/mydir/system/core/core.functions.php(637) : eval()'d code on line 7

Warning: Invalid argument supplied for foreach() in /home/mydir/system/core/core.functions.php(637) : eval()'d code on line 7

FYI, here’s the HTML form:

<form name="form3" enctype="multipart/form-data" method="post" action="/actions/doupload">
            <input type="file" size="32" name="my_field[]" value="" /></p>
            <input type="file" size="32" name="my_field[]" value="" /></p>
            <input type="file" size="32" name="my_field[]" value="" /></p>
            <input type="submit" name="Submit" value="upload" /></p>
        </form>
       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Small addition/clarification: the upload form itself is a template as well. I’m thinking the errors I detailed above are not so much the variable not getting passed, but that the files themselves are not getting passed so the array is not defined?

       
Greg Aker's avatar
Greg Aker
6,022 posts
16 years ago
Greg Aker's avatar Greg Aker

DEA:

I honestly would not be doing this in a template. Look at making a module to accomplish it. $FNS->form_declaration() is a very helpful tool that provides you with a more secure form.

That being said, I would first do a var_dump($_FILES); exit; to see if there is anything in the array.

I’m going to move this thread down to the modules forums, as it’s a better fit & I’d like to encourage you to make this into a module. 😉

-greg

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Thanks… I can look into making it a module, but the immediate question of what happens to the uploaded file remains. I’ve checked with is_array (nope) and var_dump (NULL). I swear something in EE is preventing the files from being passed.

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Er, OK. Turns out that NO form content is moving at all. I added a standard text field to the form now and tried displaying it from the $_POST[] array. I am getting the same error with that field as well now!

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Progress.

I was testing this in a hidden template group, prefaced with a “_”. Dropped the same form into a different template into a ‘standard’ template group and there was my data again!

       
DEA's avatar
DEA
257 posts
16 years ago
DEA's avatar DEA

Is there an explanation for this behaviour?

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.