上传图片" webroot / img"蛋糕php中的文件夹

时间:2014-08-24 19:00:22

标签: php cakephp

  

"我想在蛋糕php的webroot / img文件夹中上传图片。但是我无法     建议我一些解决方案,因为我是新的蛋糕php.I尝试了很多我的解决方案      但没有任何作用。所以帮助我"

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
            )

         )

     )`

1 个答案:

答案 0 :(得分:0)

请参阅:http://www.w3schools.com/php/php_file_upload.asp

需要查看以下内容:

  1. 表格attriubute必须设置为enctype =&#34; multipart / form-data&#34;
  2. move_uploaded_file($ _ FILES [&#34;文件&#34;] [&#34; tmp_name的值&#34],   &#34; IMG /&#34; 。 $ _FILES [&#34;文件&#34;] [&#34;名称&#34;]);
  3. 您还可以使用$ _SERVER [&#39; DOCUMENT_ROOT&#39;]更改上述功能中的路径。