CakePHP 1.1 File Upload Plays Rough With PHP 4

Uploading files via CakePHP has been covered a few times on Labs. Based on the feedback, it’s safe to assume bridging the gap between file uploads and PHP can be tricky. Usually Cake is ready to lend a helpful hand. Not this time.

//If you're finding that executing this:
<?php echo $html->formTag('/' . $params['url']['url'], 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $html->input('Test/title'); ?>
<?php echo $html->file('Test/file'); ?>
<?php echo $html->submit('Save'); ?>

//... returns something like this:
$this->data:Array
(
[Test] => Array
(
[title] => Foo
[file] => 4tmp/phpHGSjDA
)
)

//... instead of something like this:
$this->data:Array
(
[Test] => Array
(
[title] => Foo
[file] => Array
(
[name] => aeo.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phppM8tfc
[error] => 0
[size] => 45471
)
)
)

//... then you must be as annoyed as I am.

The reason why the file form field is returning the tmp name is because CakePHP expects register_globals is OFF.
Solution One
Turn off register_globals via .htaccess:

#/app/webroot/.htaccess
php_flag register_globals off

Solution Two
Manually write your HTML form fields without using an input array:

// do this

// not this
<?php echo $html->file('Test/file'); ?> // will output:

Solution two is a slight pain in the ass, because now that you’ve eliminated the array within the form input field, you need to do a little extra work in your CakePHP controller. (If there’s a demand, I’ll give some examples.)
Hopefully this’ll clear up Cake’s messhole.