"我想在蛋糕 php 的 webroot/img 文件夹中上传图像,但我无法 建议我一些解决方案,因为我是蛋糕PHP的新手。我从我的 但没有什么 works.so 帮助我">
my HomesController.php code as
public function admin_add() {
if ($this->request->is('post')) {
$this->Home->create();
$image = $this->request->data['Home']['image'];
//$imageTypes = array("image/gif", "image/jpeg", "image/png");
//$uploadFolder = "upload";
//$uploadPath = WWW_ROOT . $uploadFolder;
pr($this->request->data);
//echo $image['type'];
if ($this->Home->save($this->request->data)) {
$this->Session->setFlash(__('The home has been saved.'));
//echo pr($this->request->data);
//echo die(debug($this->request->data));
//return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The home could not be saved., tryagain '));
}
}
}
admin_add.ctp 代码是
<div class="homes form">
<?php echo $this->Form->create('Home', array('type' => 'file')); ?>
<fieldset>
<legend><?php echo __('Add Home content'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->input('image');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));
?>
</div>
块引用
运行代码时,我无法上传图像,并且在打印图像数据时我收到错误。
Output:
`Array
(
[Home] => Array
(
[title] => vhbk
[body] => ,bjljbl.
[image] => Array
(
[name] => DSC03371.JPG
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
)`
请参考这个:http://www.w3schools.com/php/php_file_upload.asp
需要检查以下事项:
- 必须设置 enctype="multipart/form-data">
- move_uploaded_file($_FILES["文件"]["tmp_name"], "哼/" .$_FILES["文件"]["名称"](;
你也可以使用 $_SERVER['DOCUMENT_ROOT'] 来更改上述函数中的路径。