Jump to content

File Upload $_files empty


Frank Schneider
 Share

Recommended Posts

I have a Processwire application. In it I have two areas company and their employees. I insert an image in Employees and upload it, it works fine so far, now I do the same in the company and want to use it as a logo, it doesn't work there. When I click save, the $_files field is empty, but not for the employee. So exactly the same as with the employee does not work, same fields, same code.

The field is defined in fields as Image and assigned to the template

I've searched the whole internet but can't find the reason.

my code:

 
$form_fields = array(
    .....
    'foto' => array('type' => 'file', 'required' => true),
);
// --- WireUpload settings ---
$upload_path = $config->paths->assets . "files/.tmp_uploads/"; // tmp upload folder
$file_extensions = array('jpg', 'jpeg', 'gif', 'png');
$max_files = 1;
$max_upload_size = 1*1024*1024; // make sure PHP's upload and post max size is also set to a reasonable size
$overwrite = false;
$update_page        = ($page->url == $pages->get("/firmenverwaltung/edit/")->url) ? $pages->get("id=".(int)$input->urlSegment0) : null;
$file_field = "foto";
 
 
//HTML
        <div class="col-lg-6">
                    <div class="form-group">
                        <label class="form-control-label">Logo: <?php if ($form_fields['foto']['required']) { ?><span class="tx-danger">*</span><?php } ?></label>
                    
                        <div class="<?php if ($errors["foto"]) { echo "parsley-error"; } ?> custom-file">
                            <input id="foto" type="file" name="foto[]">
                        </div>
                        
                        <?php showError($errors, "foto"); ?>
                    </div>
                    
                    <?php
                    if ($firma)
                    {
                        $foto = null;
                        foreach ($firma->foto as $image)
                        {
                            $foto = $image->width(250);
                            break;
                        }
                        if ($foto != null)
                        {
                            echo "<img src='".$foto->url."' />";
                        }
                    }
                    ?>
                </div>
 
//Save
if($input->post->action == 'send')
{
 
// first validate the CSRF token to check if it is a valid request
if(!$session->CSRF->hasValidToken()){$errors['csrf'] = "Form submit was not valid, please try again."; }
 
  //Look for required fields and make sure they have a value
foreach($required_fields as $req)
{
        //print_r($_FILES);
        //die;
        
// required file upload field
if($form_fields[$req]['type'] == 'file'){if(empty($_FILES[$req]['name'][0])){$errors[$req] = "Wählen Sie Dateien zum Hochladen aus.."; }
            
  } else if($form_fields[$req]['type'] == 'checkbox'){if($form_fields[$req]['value'] == 0){$errors[$req] = "field necessary.";}
        
// erforderliche Textfelder
} else if($form_fields[$req]['type'] == 'text'
|| $form_fields[$req]['type'] == 'textarea'
|| $form_fields[$req]['type'] == 'email')
{
if(!strlen($form_fields[$req]['value'])){$errors[$req] = "Feld notwendig."; }
 
// reqired email fields
if($form_fields[$req]['type'] == 'email')
{
if($form_fields[$req]['value'] != $input->post->$req){$errors[$req] = "Bitte eine gültige E-Mail Adresse angeben.";}
}
}
}
 
 
Link to comment
Share on other sites

To me this looks like a redirect issue. Thing is, that $_FILES gets cleared when POST requests are redirected. You can check for redirects in your browser dev tool's network tab when you submit the form. 

If you use the exact same form for both submissions (company and employee), you need to check what is different when you submit the form. Is the form action URL for both exactly the same? Do you have any code in place that redirects the form submission to a different URL when using it for employee?

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...