<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yzxh24
 * Date: 11-10-12
 * Time: 下午1:08
 * To change this template use File | Settings | File Templates.
 */
class UploadImageAction extends BaseAction
{
    public function run()
    {
        $id = Yii::app()->request->getQuery('id');
        $soft = SoftContent::model()->findByPk($id);
        if (empty($soft))
        {
            echo '不存在的软件';
            Yii::app()->end();
        }

        $soft->Filedata = CUploadedFile::getInstance($soft, 'Filedata');

        $fileExt = CFileHelper::getExtension($soft->Filedata->getName());
        $newFileName = Helper::guid() . '.' . $fileExt;
        $fileName = $soft->Filedata->getName();

        $savePath = Front::getSoftBaseSavePath(date('Y-m-d'), 'image');
        if (false === $savePath)
        {
            echo '文件保存目录不可写';
            Yii::app()->end();
        }
        $saveFile = $savePath . $newFileName;
        $soft->Filedata->saveAs($saveFile);
        $soft->images = $soft->images + 1;
        $soft->save();

        $cpic = new SoftCpic();
        $cpic->date = date('Y-m-d');
        $cpic->cid = $soft->id;
        $cpic->name = $newFileName;
        $cpic->save();

        echo 'FILEID:'.$soft->id;
        Yii::app()->end();
    }
}