如何在cakephp中将图像加载到文件夹和DB的路径

时间:2017-02-08 07:13:37

标签: cakephp

我已经在cakephp控制器中编写了一些代码来将图像存储在文件夹和db的路径中,我的代码正在工作,因为图像名称与所有其他字段一起存储在数据库中。但是图像没有存储在文件夹中,路径是不存储在db.only中的上传图像的名称正在存储。

这是我的控制器代码

  function addAdminList(){
    $this->layout = "ajax";
    if($this->request->data){
        $postArr = $this->request->data;
        $Curriculum = array();
        if($postArr['branchId'] > 0){
            $Curriculum['Shop']['id'] = $postArr['branchId'];
        }
        $Curriculum['Shop']['name'] = $postArr['branchName'];
        $Curriculum['Shop']['parent_id'] = $postArr['curiculumName'];

        $Curriculum['Shop']['owner'] = $postArr['owner'];
        $Curriculum['Shop']['startdate'] = $postArr['startdate'];
        $Curriculum['Shop']['phone'] = $postArr['phone'];
        $Curriculum['Shop']['desc'] = $postArr['desc'];
        $Curriculum['Shop']['address'] = $postArr['address'];
        $Curriculum['Shop']['timings'] = $postArr['timings'];
        $Curriculum['Shop']['image'] = $postArr['image'];
        $file = $postArr['image'];
        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
        $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
        if(in_array($ext, $arr_ext))
    {
     //do the actual uploading of the file. First arg is the tmp name, second arg is
        //where we are putting it
        if(move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/upload_folder' . DS . $file['name']))
        {
            //prepare the filename for database entry
            $this->request->data['Shop']['image'] = $file['name'];
            pr($this->data);
            if ($this->Shop->save($this->request->data)) 
            {
                $this->Session->setFlash(__('The data has been saved'), 'default',array('class'=>'success'));

            }
            else
            {
                $this->Session->setFlash(__('The data could not be saved. Please, try again.'), 'default',array('class'=>'errors'));
            }
        }
    }


        $this->Shop->save($Curriculum);
        echo 0;
        exit;
    }
}

,表单代码为

<form id="addClsGrpFrm" method="post" action="#" onsubmit="return disableFrmSubmit()">
        <div class="flash_msg"><?php echo $this->Session->flash(); ?></div>
        <input type="hidden" name="data[branch][id]" readonly id="branchId" value="0">
        <div class="login-form">
            <label>Select Shop Under</label>
            <select id="curiculumName" data-prompt-position="topLeft:200" name="data[branch][curiculumName]" class="validate[required]">
                <option value="">Please Select shop</option>
                <?php if(!empty($curriculumArr)){
                    foreach($curriculumArr as $res){
                        $id = $res['Shop']['id'];
                        $name = $res['Shop']['name'];?>
                        <option value="<?php echo $id;?>"><?php echo $name;?></option>
                    <?php }
                }?>
            </select>

            <label>Shop Name</label>
            <input type="text" id="branchName" data-prompt-position="topLeft:200" name="data[branch][branchName]" placeholder="shop Name" class="validate[required]" />
        <label>Shop Image</label>
            <input type="file" id="image" data-prompt-position="topLeft:200" name="data[branch][image]" placeholder="" class="validate[required]" />
        <label>Owner Of Shop</label>
            <input type="text" id="owner" data-prompt-position="topLeft:200" name="data[branch][owner]" placeholder="Owner Name" class="validate[required]" />  

            <label>Shop Started Date</label>
            <input type="text" id="startdate" data-prompt-position="topLeft:200" name="data[branch][startdate]" placeholder="yyyy-mm-dd" class="validate[required]" />


            <label> Phone Number</label>
            <input type="text" id="phone" data-prompt-position="topLeft:200" name="data[branch][phone]" placeholder="Phone Number" class="validate[required]" />

                <label class="lable1 fl">Content Description<sup>*</sup></label>
                <textarea id="desc" name="data[content][desc]" class="areaClass studentarea1 fr validate[required]" maxlength="255" ></textarea>

            <label class="lable1 fl">Address<sup>*</sup></label>
                <textarea id="address" name="data[branch][address]" class="areaClass studentarea1 fr validate[required]" maxlength="255" ></textarea>


            <label class="lable1 fl"> Timings</label>
            <input type="text" id="timings" data-prompt-position="topLeft:200" name="data[branch][timings]" placeholder="9:00 Am - 1:00 PM | 3:00 PM -9:00 PM " class="validate[required]" />   
             </div>
                <div class="login-form">
                    <input type="button" onclick="addAdminList()" class="login-btn" value="Submit">
                </div>
        <div class="clear"></div>
    </form>

如何上传图像到文件夹和路径到我的db表。在我的代码中编辑什么来做这个plz帮助我。

0 个答案:

没有答案