I’ve been searching through forums (“file upload”, “upload”, “blob”) but haven’t found the solution. I also tried to implement solution by quiang’s Wiki Tutorial http://www.yiiframew…-using-a-model/ without success.
‘cv_file_name’ is input for file browsing, and file input is done by CHtml::activeFileField as said in Wiki…
In model I have:
<?php
class Candidate extends CActiveRecord
{
public $uploaded_file;
.....
.....
public function rules()
{
array('cv_file_name', 'file', 'allowEmpty'=>false),
}
?>
In Controller I have:
public function actionCreate()
{
$model=new Candidate;
if(isset($_POST['Candidate']))
{
$model->attributes=$_POST['Candidate'];
//some diagnostic stuff
echo '<pre>'.$model->cv_file_name.'<br>';
echo '<pre>blahblahblah<br>';
//some diagnostic stuff
$uploaded_file = CUploadedFile::getInstance($model,'cv_file_name');
//some diagnostic stuff to see if $file exists
echo '<pre>echo file here: '.$file.' :echo file here<br>';
//some diagnostic stuff to see if $file exists
$model->cv_file_name=$uploaded_file->name;
$model->cv_file_type=$uploaded_file->type;
$model->cv_file_size=$uploaded_file->size;
$model->cv_file_content=file_get_contents($uploaded_file->getTempName());
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
I put some diagnostic messages there…
And I get:
/protected/controllers/CandidateController.php on line 81
This line: $model->cv_file_content=file_get_contents($uploaded_file->getTempName());
[color="#ff0000"]Please[/color], I have been messing with this stuff… ehhhhhh, toooooo long.
I used your code and now I don’t get error that trying to work on non-object, but now I get that my cv_file_name (input for file) is blank. No matter if I use:
I tried lot of time ago (still was Yii 1.0) and I did everithing in the model.
I added a property to be set with CFileUpload, and I change the class like that:
<?php
class photograph extends CActiveRecord
{
/* property for receive the new file*/
public $preview;
/**
*saves the data and the type of the uploaded image
*/
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'preview'))
{
$this->preview_data=file_get_contents($file->tempName);
$this->preview_mime=$file->type;
$this->resizeImage();
}
return parent::beforeSave();;
}
}
class Candidate extends CActiveRecord
{
/* PROPERTY FOR RECEIVING THE FILE FROM FORM*/
public $cv_file_name;
/* MUST BE THE SAME AS FORM INPUT FIELD NAME*/
/**
*saves the name, size ,type and data of the uploaded file
*/
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'cv_file_name'))
{
$this->cv_file_name=$file->name;
$this->cv_file_type=$file->type;
$this->cv_file_size=$file->size;
$this->cv_file_content=file_get_contents($file->tempName);
}
return parent::beforeSave();;
}
File data is properly uploaded to blob, name, size and type to appropriate columns.
class Candidate extends CActiveRecord
{
/* PROPERTY FOR RECEIVING THE FILE FROM FORM*/
public $uploadedFile;
/* MUST BE THE SAME AS FORM INPUT FIELD NAME*/
/**
*saves the name, size ,type and data of the uploaded file
*/
public function beforeSave()
{
if($file=CUploadedFile::getInstance($this,'uploadedFile'))
{
$this->cv_file_name=$file->name;
$this->cv_file_type=$file->type;
$this->cv_file_size=$file->size;
$this->cv_file_content=file_get_contents($file->tempName);
}
return parent::beforeSave();;
}
Also I had to change blob to mediumblob, because blob truncated file to 64kB.
Well it’s nice to have this solution - how about putting this in Wiki (there is on tutorial, but about uploading file to filesystem) - who should I contact in this case?
Now I am working on displaying link to the blob’ed file and open it onClick(). Have done it once but in pure PHP, not in Yii, this is my next milestone.
As said pc131, follow the wiki and if you find something incorrect, please say us so we can correct the wiki (or correct you immediatly if you are sure)