- "The documentation for CMultiFileUpload isn't clear!"
- "I don't know where the uploaded files went!"
- "How can I save those files I uploaded in the right directory!"
- "!@!##!!!!"
I went through a weeks worth of frustration that was unnecessary, so for your benefit, here are the steps towards the uploading of images via CMultiFileUpload so you can focus on the more important thing: your actual work.
- Database - a user table, and a picture table, with the picture table linking (via a foreign key) back to the user table. More simplified versions (just uploading straight to a picture table) can be easily discerned via this specialized example.
- Relations - So, given the above assumption, through the 'relations' function that gii (for example) auto-creates for you, you will be able to access your pictures like so: $model->pictures
View
In _form.php: - You have to add "multi-part/form-data" to your _form.php file, by two methods: either add the following modification to your header
$this->widget('CMultiFileUpload', array( 'name' => 'images', 'accept' => 'jpeg|jpg|gif|png', // useful for verifying files 'duplicate' => 'Duplicate file!', // useful, i think 'denied' => 'Invalid file type', // useful, i think ));
Controller
Algorithm:- load the model in question
- check if the model is set
- get your uploaded images
- save each image in the appropriate place on your server
- create new instantiation of your picture model
- save that instantiation
- save the rest of the data you used in your form
- onto the next headache
// make the directory to store the pic: if(!is_dir(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)) { mkdir(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)) chmod(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)), 0755); // the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here's less of a headache in case you don't }
$model=$this->loadModel($id); // Uncomment the following line if AJAX validation is needed //$this->performAjaxValidation($model); if(isset($_POST['Topic'])) { $model->attributes=$_POST['Topic']; // THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'images' $images = CUploadedFile::getInstancesByName('images'); // proceed if the images have been set if (isset($images) && count($images) > 0) { // go through each uploaded image foreach ($images as $image => $pic) { echo $pic->name.' '; if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'.$pic->name)) { // add it to the main model now $img_add = new Picture(); $img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model $img_add->topic_id = $model->id; // this links your picture model to the main model (like your user, or profile model) $img_add->save(); // DONE } else // handle the errors here, if you want } // save the rest of your information from the form if ($model->save()) { $this->redirect(array('view','id'=>$model->id)); } } } $this->render('update',array('model'=>$model,));
Hat-tip: Wiseon3 caught a copy/paste error of mine:
"if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $model->name)) { // add it to the main model now"
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/ADD YOUR PATH HERE!/'. $pic->name)) { // add it to the main model now
You have any image upload that has drag and drop functionality?
ReplyDeletePulseStore Forceps * efyohsikei