简体   繁体   English

在文件上传操作期间将数据保存在yii2中的数据库中

[英]Saving data in the database in yii2 during file upload action

I have a upload controller where by am also performing saving other data to the database. 我有一个上传控制器,通过该控制器还可以将其他数据保存到数据库中。 The file uploading to the folder is okay but saving the other details in the table doesn't happen 可以将文件上传到文件夹中,但是不会将其他详细信息保存在表中

controller code

$images = $_FILES['evidence'];
$success = null;

$paths= ['uploads'];

// get file names
$filenames = $images['name'];


// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
    $success = true;
    $paths[] = $target;
} else {
    $success = false;

    break;
}

echo $success;
}
// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $evidence->save();

$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];

foreach ($paths as $file) {
    unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}

//  return a json encoded response for plugin to process successfully
echo json_encode($output);

After doing var_dump($evidence->save()) I get an error of Boolean false 在执行var_dump($evidence->save())我得到布尔值false的错误。

You could have validation errors. 您可能有验证错误。 Check $errors property 检查$errors属性

// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $retSave = $evidence->save();

        if($retSave == false)
        {
              var_dump($evidence->errors);
        }

$output = [];
}
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM