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 http://milesj.me/code/cakephp/uploader . 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;
<?php
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;
_method=POST&_method=POST&data%5BUpload%5D%5Bpicture%5D=SSSS.jpg
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
?
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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.