Saving Docx Content Along With Binary File

Hello

I am saving docx file as binary to database BLOB. I would like to also save the content of the file to be able to search for phrases in doc without opening it. I found PHP code to read docx file and it works. Now - how to incorporate this while saving form on which I have file input field?

The form has string fields and on file input field ‘cv’.

Here is my controller code:




public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Candidate']))

		{

			if($_FILES['Candidate']['name']['cv']=='')

			{

				$model->name=$_POST['Candidate']['name'];

				$model->surname=$_POST['Candidate']['surname'];

				$model->id_candidate_avail=$_POST['Candidate']['id_candidate_avail'];

				$model->id_candidate_src=$_POST['Candidate']['id_candidate_src'];

				$model->id_sector=$_POST['Candidate']['id_sector'];

				$model->id_subsector=$_POST['Candidate']['id_subsector'];

				$model->address_main=$_POST['Candidate']['address_main'];

				$model->address_code=$_POST['Candidate']['address_code'];

				$model->address_city=$_POST['Candidate']['address_city'];

				$model->mobile=$_POST['Candidate']['mobile'];

				$model->phone=$_POST['Candidate']['phone'];

				$model->email=$_POST['Candidate']['email'];

				$model->comments=$_POST['Candidate']['comments'];

				$model->save(true,array('name','surname','id_candidate_avail','id_candidate_src','id_sector','id_subsector','address_main','address_code','address_city','address_region','mobile','phone','email','comments'));

			}

			

			else

			{

				$model->attributes=$_POST['Candidate'];

				$model->save();			

			}

			

			$file=CUploadedFile::getInstance($model,'cv');

			if($file)

			{

				if($_FILES['Candidate']['name']['cv']!=''){

				$cv_file_content=substr(file_get_contents($file->tempName),0,200);

				$cv_file_type=$file->type;

				$cv_file_size=$file->size;

				$cv=$file->name;

				}														

			}	


			$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}



It works, it saves docs to database, it updates them with new files.

Now how to add mentioned earlier code to read docx before saving/updating and save it to new text field in DB?

Thanks in advance

I do not have an answer to your question, but I would just like to mention that it is not considered to be good practise to store files in the database. It is better to store them in a folder and store a reference in the database.

I read much about putting files as BLOBS into DB and storing them as links to the files on filesystem. Finally I decided to go DB path, but may consider the change. What is the biggest issue? DB size when much files uploaded? Positive is that files are always there…

Anyway I found the solution to read docx file content, before saving it to DB

I added to controller a function called "read_file_docx", which returns me plain text from docx




public function read_file_docx($filename)

{

    $striped_content = '';

    $content = '';

    if(!$filename || !file_exists($filename)) return false;

    $zip = zip_open($filename);

    if (!$zip || is_numeric($zip)) return false;

		while ($zip_entry = zip_read($zip)) {

        if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

        if (zip_entry_name($zip_entry) != "word/document.xml") continue;

        $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

        zip_entry_close($zip_entry);

		}

    zip_close($zip);

    $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);

    $content = str_replace('</w:r></w:p>', "\r\n", $content);

    $striped_content = strip_tags($content);


    return $striped_content;

}



then I call this function in other function in controller




public function actionUpdate($id)

        {

                $model=$this->loadModel($id);


                // Uncomment the following line if AJAX validation is needed

                // $this->performAjaxValidation($model);


                if(isset($_POST['Candidate']))

                {

                        if($_FILES['Candidate']['name']['cv']=='')

                        {

                                $model->name=$_POST['Candidate']['name'];

                                $model->surname=$_POST['Candidate']['surname'];

                                .....

                                ....

                                $model->email=$_POST['Candidate']['email'];

                                $model->comments=$_POST['Candidate']['comments'];

                                $model->save(true,array('name','surname','id_candidate_avail','id_candidate_src','id_sector','id_subsector','address_main','address_code','address_city','address_region','mobile','phone','email','comments'));

                        }

                        

                        else

                        {

                                $model->attributes=$_POST['Candidate'];

                                $model->save();                 

                        }

                        

                        $file=CUploadedFile::getInstance($model,'cv');

                        if($file)

                        {

                                if($_FILES['Candidate']['name']['cv']!=''){

///// CALLING READ_FILE_DOCX ////////////////////////////////////////////////////CALLING READ_FILE_DOCX ////

                                $this->cv_file_text=$this->read_file_docx($file->tempName);

///// CALLING READ_FILE_DOCX ////////////////////////////////////////////////////CALLING READ_FILE_DOCX ////

                                $cv_file_content=file_get_contents($file->tempName);

                                $cv_file_type=$file->type;

                                $cv_file_size=$file->size;

                                $cv=$file->name;

                                }                                                                                                               

                        }       


                        $this->redirect(array('view','id'=>$model->id));

                }


                $this->render('update',array(

                        'model'=>$model,

                ));

        }



And it works. It saves the content of *.docx file. Hope someone may find that helpful.