咔咔咔 2014-02-13 17:45:01 18981次浏览 3条评论 11 2 0

//在_form.php代码

<?php 
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'product-form',
    'enableAjaxValidation'=>false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>

<!--js代码-->
<script>
    //多图片上传
    $(function()
    {
        var j=1;
        $('#add_file').click(function()
        {
            $('#add_file').before("<div class='upimg'><input type='file' name='imageFile"+j+"'></div>");
            j++;
        })
     });
    //多图片删除
    $(function(){
        $('.a_de').click(function(){
        var the_a=$(this);
        var ima_id=$(this).children(':input').val();
        $.post("<?php echo $this->createUrl('Product/deleteIma')?>","ima_id="+ima_id+"",function(a){
            if(a==1){
                the_a.parent().remove();
            }
        })
        })
    })
</script>
<!--js代码-->


<!--上传图片开始 -->
 <div class="row pos" id="img">
    <?php echo $form->labelEx($model,'images'); ?>
    <input type="file" name="imageFile"/>
    <a href="javascript:viod()" id="add_file">上传多张图片</a>
</div>
<div>
<?php
    if($model->id!=null){
       $image= Proimg::model()->findAll("pid=:pid",array(":pid"=>$model->id));
       if($image==null){
           echo "<p style='color:red'>暂无图片</p>";
       }else{
           echo "<br>";
           $url=explode("/",Yii::app()->baseUrl);
           foreach ($image as $ima)
           {
               echo "<div class='ima'><img src='".Yii::app()->request->baseUrl.$ima->url."' height='48' width='72'><br>";
               echo "<a class='a_de' href='javascript:viod(0)'>删除<input type='hidden' value='".$ima->id."'></a>";
               echo "</div>";
            }
        }

    }
?>
</div>
<!--上传图片结束-->

//在控制器ProductController.php代码

上传图片:

public function actionCreate()
{
    $model=new Product;
    if(isset($_POST['Product']))
    {
        $model->attributes=$_POST['Product'];
        if($model->save()){
            <!-- 上传图片函数

            if($_FILES['imageFile']['name']!=null)
            {
                $model->setImageInformation($_FILES);
            }
            -->
        } 
    } 
    $this->render('create',array(
    'model'=>$model,
    ));
}

删除图片:

public function actionDeleteIma()
{
    $ima_id=$_POST['ima_id'];
    $image_info=Proimg::model()->findByPk($ima_id);

    $img_url= "..".Yii::app()->request->baseUrl.$image_info['url'];
    if(file_exists($img_url)){
        unlink($img_url);
    }
    $a=$image_info->delete();
    echo $a;
}

//在models文件下product.php里代码

//图片函数
public function setImageInformation($image){
    foreach($image as $file){
        $name=$file['name'];
        $arr=explode('.',$name);
        $ext=$arr[count($arr)-1];
        $root = "..".Yii::app()->request->baseUrl;//echo dirname(__FILE__)
        $path="/img/proimg/".date("YmdHis").mt_rand(1,9999).".".$ext;
        move_uploaded_file($file['tmp_name'],$root.$path);
        <!--写入数据库 按自己的需求更改
        $db = Yii::app()->db;
        $db->createCommand()->insert('{{proimg}}', array(
            'pid' => $this->id,
            'url'=>$path,
            'createtime'=>date('Y-m-d H:i:s'),
        ));
        -->
    }
}
觉得很赞
您需要登录后才可以评论。登录 | 立即注册