我创建了一个添加用户的功能。参数是用户名,电子邮件,密码和个人资料图片。
<form name="User" method="post" action="http://192.168.1.100/filmtastic/api/users/adduser" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td></tr></table>
</form>
现在我的UserController
我有:
public function api_adduser() {
$this->layout = false;
$this->request->data['User']= $this->request->data;
if($this->request->data['User'] != array()) {
pr($this->request->data); die();
}
}
这里我调试由该HTML表单传递的数据,optput就像
Array
(
[username] => jack roy
[password] => jack
[email] => jack@yahoo.com
[submit] => Submit
[User] => Array
(
[username] => jack roy
[password] => jack
[email] => jack@yahoo.com
[submit] => Submit
)
)
问题是它不会向我显示图像数组。你能告诉我怎么会得到那个看起来像
的图像数组Array
(
[image] => Array
(
[name] => 06_01_2009_0692878001231258160_nanzig.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\php368F.tmp
[error] => 0
[size] => 81167
)
)
答案 0 :(得分:0)
也许您只是忘了将表单mime类型设置为multipart/form-data
。这是我的演示视图,名为add.ctp
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->password('password');
echo $this->Form->input('email');
echo $this->Form->file('image');
echo $this->Form->submit();
echo $this->Form->end();
以下是UserController
class UserController extends AppController {
public $helpers = array('Html', 'Form');
public function add() {
pr($this->request->data);
}
}
以下是提交表单
时的结果Array
(
[User] => Array
(
[name] => admin
[password] => admin
[email] => asdf@qwerty.com
[image] => Array
(
[name] => cakephp-book.odt
[type] => application/vnd.oasis.opendocument.text
[tmp_name] => /tmp/phpKniokr
[error] => 0
[size] => 170247
)
)
)
以下是您可能希望在外部页面上使用的生成HTML的转储(请问为什么??? )。请注意FormHelper
添加了多少内容...我想知道如果您的应用程序仍然直接访问您的服务器,为什么要构建外部视图...
<form action="/test/cakedemo/user/add" enctype="multipart/form-data" id="UserAddForm"
method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<div class="input text">
<label for="UserName">Name</label><input name="data[User][name]"
id="UserName" />
</div><input name="data[User][password]" type="password" id="UserPassword" />
<div class="input text">
<label for="UserEmail">Email</label><input name="data[User][email]" type="text"
id="UserEmail" />
</div><input type="file" name="data[User][image]" id="UserImage" />
<div class="submit">
<input type="submit" value="Submit" />
</div>
</form>