使用路径将文件上载到sql表

时间:2014-08-01 15:05:22

标签: php mysql sql

我尝试使用路径将文件上传到MySQL表(该文件将保存在表中的服务器asd上,为他提供链接)。这就是我所做的:

我的观点是:

<div class="row">
<?php echo $form->labelEx($model,'doc_ordered_recieved'); ?>
<?php echo $form->fileField($model,'doc_ordered_recieved'); ?>
<?php echo $form->error($model,'doc_ordered_recieved'); ?>
</div>

我的模特是:

array('doc_ordered_recieved','file','types'=>'pdf', 'allowEmpty'=>true, 'on'=>'insert,update'), 

我的控制器是:

public function actionCreate()
{
$model=new Orders;


if(isset($_POST['Orders']))
{

    $model->attributes=$_POST['Orders'];
    $uploadedFile = CUploadedFile::getInstance($model,'doc_ordered_recieved');
    if($model->save())
    {
        if(!empty($uploadedFile))  // check if uploaded file is set or not
        {

            if($model->doc_ordered_recieved== null || empty($model->doc_ordered_recieved)){
                $rnd = rand(0,9999);// generate random number between 0-9999
                $fileName = "{$rnd}-{$uploadedFile}";
                $model->doc_ordered_recieved= $fileName;
            }
            $uploadedFile->saveAs(dirname(__FILE__)..'/files/'. $model->doc_ordered_recieved);
            // redirect to success page
        }
        $this->redirect(array('view','id'=>$model->oid));
    }
}
    $this->render('create',array('model'=>$model,
));
}

该表定义为varchar即可。????

该文件应保存在d:\xampp\htdocs\filse

请帮助我,总是说:doc_ordered_recieved can't be blank

0 个答案:

没有答案