1
votes

I'm trying to modify a newsletter platform written in CakePHP to make attachments. When creating a newsletter, it's possible to upload an image file which will be added as an attachment. However, after the form submission (newsletter creation is a form), the $_FILES variable is empty. The name of the file is included in the POST data though.

Here is some of the form HTML

<form action='/systeem/nieuwsbrieven/' method='POST'>

<tr>

    <td style='padding: 10px;'><textarea name='data[Nieuwsbrieven][omschrijving]' style='width: 100%;height: 150px;'></textarea></td>   

</tr>

<tr>

    <td style='padding: 10px;'><input type="file" id='fileupload' name="data[Nieuwsbrieven][attachment]"  style='width: 100%;height: 150px;'></td>  

</tr>

I added the 'data[Nieuwsbrieven][omschrijving]' because that bit works. Here is the function that processes the form:

function nieuwsbrieven() { 
    if (isset($this->data)) {
        echo 'DATA:';
        var_dump($this->data);
        echo 'FILES:';
        var_dump($_FILES);
        //echo $_FILES['data[Nieuwsbrieven][attachment]']['name']."<< THE NAME";
        exit();
    }
    else {

        echo "data bestaat niet";
    }

    if(!empty($this->data)) { 

        $this->data['Nieuwsbrieven']['datum'] = mktime();

        $datum = explode('-', $this->data['Nieuwsbrieven']['convert_datum']);           

        $this->data['Nieuwsbrieven']['plan_datum'] = mktime(0, 0, 0, $datum[1], $datum[0], $datum[2]);

        $this->Nieuwsbrieven->save($this->data);
        //$this->redirect("/systeem/verzenden/"); 

    }

}

As you can see, I var_dump the $this->data and $_FILES, here is the output of that:

DATA: array(1) { ["Nieuwsbrieven"]=> array(5) { ["content"]=> string(21) "
test content

" ["titel"]=> string(12) "test subject" ["convert_datum"]=> string(10) "25-09-2014"     ["omschrijving"]=> string(4) "test" ["attachment"]=> string(16) "137785222989.jpg" } } FILES: array(0) { }

It's not a problem with INI settings because I tried a test script with vanilla PHP and was able to upload files just fine.

3

3 Answers

2
votes

a much better option would be using cakephp's Form helper, you should pass type parameter with file instead of usual post

  echo $this->Form->create('User', array('type' => 'file'));

Please check http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create

1
votes

Try to declare form with following form :

HTML

<form action='/systeem/nieuwsbrieven/' method='POST' enctype="multipart/form-data">

CakePHP

echo $this->Form->create('User', array('type' => 'file'));
0
votes

you miss the enctype ..

when your form includes any elements add multipart/form-data with your form tag. like : <form action='/systeem/nieuwsbrieven/' method='POST' enctype="multipart/form-data">