2014-07-04 03:04


  • file-upload
  • http-post
  • php

I am trying to create a simple Cakephp 2.5.1 app that accepts file upload. I am using the file uploader plugin provided in I followed the instructions in the website and got it installed and it seems to work fine.

The code below is added to the model.php. There is a column 'picture' in the model table.

public $actsAs = array(
    'Uploader.Attachment' => array(
        // Do not copy all these settings, it's merely an example
        'picture' => array(
            'nameCallback' => '',
            'append' => '',
            'prepend' => '',
            'tempDir' => TMP,
            'uploadDir' => '',
            'transportDir' => '',
            'finalPath' => '',
            'dbColumn' => '',
            'metaColumns' => array(),
            'defaultPath' => '',
            'overwrite' => false,
            'stopSave' => true,
            'allowEmpty' => true,
            'transforms' => array(),
            'transformers' => array(),
            'transport' => array(),
            'transporters' => array(),
            'curl' => array()

In the associated ctp file, I have the following code;

    echo $this->Form->create('Upload', array('type' => 'file'));
    echo $this->Form->input('picture', array('type' => 'file'));
    echo $this->Form->end('Submit');

I managed to upload an image file SSSS.jpg to the webserver. However, the strange thing is that the uploaded image file is a tiny file which contains the HTTP POST information in text, not the actual image data. The file contents looks something like this;


Can someone tell me what are the possible things that have gone wrong? Thank you.

EDIT: I just noticed in Chrome debugging that under the HTTP POST method, the file sent was of type text/html. Is this a problem? If yes, how do I change the type to image/jpg ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanji5746 duanji5746 7年前

    Change the following line

    echo $this->Form->input('picture', array('type' => 'file'));

    To the following syntax

    echo $this->Form->input('picture', array('type' => 'file/image'));

    Sample Code:

    echo $form->labelTag('File/image', 'Image');
    echo $html->file('File/image');
    点赞 评论 复制链接分享