Question

Please Help Me.

Get uploaded images while updating/editing in yii

In CRUD operation

when i upload files in cmultifile working fine while creation and deletion, but my problem is during the updating of that particular form.

If any suggestion to keep that drop down field and files in Cmultifile always set based on database values.

Please Help me for CMultiFileUpload widget...getting files from database as selected

Hers my code

In Form used this widget

  <?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'upload-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
    'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),
)); ?>


<?php  $this->widget('CMultiFileUpload',
  array(
       'model'=>$model,
       'attribute' => 'Document',
       'accept'=>'jpg|gif|png|doc|docx|pdf',
       'denied'=>'Only doc,docx,pdf and txt are allowed', 
       'max'=>4,
       'remove'=>'[x]',
       'duplicate'=>'Already Selected',

       )
        );?>

In Controller

  public function actionCreate()

    {
        $model = new Upload;
       echo Yii::app()->basePath.'/Images/';
       if(isset($_POST['Upload']))
        {
    if($filez=$this->uploadMultifile($model,'Document','/Images/'))
   {

   $model->Document=implode(",", $filez);
   }
   $model->attributes=$_POST['Upload'];
    if ($model->save())
            {
               $this->redirect(array('view', 'id' => $model->idUpload));
        }
        }
        $this->render('create', array(
            'model' => $model,
         ));

    }
//Function for uploading and saving Multiple files
    public function uploadMultifile ($model,$attr,$path)
    {
    /*
     * path when uploads folder is on site root.
     * $path='/uploads/doc/'
     */
    if($sfile=CUploadedFile::getInstances($model, $attr)){

      foreach ($sfile as $i=>$file){  

        // $formatName=time().$i.'.'.$file->getExtensionName();
        $fileName = "{$sfile[$i]}";
          $formatName=time().$i.'_'.$fileName;
         $file->saveAs(Yii::app()->basePath.$path.$formatName);
         $ffile[$i]=$formatName;
         }
        return ($ffile);
       }
     }
Was it helpful?

Solution

File fields are different then classic text fields... it's because a file field does not only have the filename in the field... but a file to be uploaded to... you can think of them as they are connected... so you cannot pre-populate a file field with the file name.

To explain the workflow:

  • a user enters some non-valid data for any other field, chooses the file to be uploaded and submits the form
  • at the submit time the HTTP protocol sends the form data togetherwith the chosen file to the server
  • at that time in your action you can access the file with $_FILE that resides in some temporary server folder
  • now... if the validation fails you (we) are displaying (rendering) again the input form, in other words we are sending to the client browser a new form to be shown with the validation errors
  • so as the users gets this new form even if you could pre-populate the previous chosen file name... you cannot in any way pre-populate the file content to be uploaded

There are few solutions that I can think of

  • use ajax/client validation so to be sure that when the form is submited, all the fields will pass the validation.
  • make the picture upload separate (that is the reason why most software like forums have the avatar/profile picture upload in a separate form)

There is some more advanced solutions but they resemble the above second method... like having the whole form with the file upload together and then when the file is selected to upload that file with ajax even before the form is submited... but that is the most complicated and has some more thinking about it as the user can decide to not submit the form but the picture is already uploaded.

Posted From : File Field

I Solved using javascript append the file of that record and hide when selects new file. hence as per now not found perfect solution. Hers the code

To Display Files

<?php 
                    if($model->register_id != Null || $model->register_id != "")
                        {
                            $attribute=CHtml::encode($model->medical_documents);
                            $file = str_getcsv($attribute ,",");
                            ?> <div id="UploadedFiles"> Uploaded Files: <?php  
                            foreach($file as $i=> $record)
                            { ?>
                            <div>
                            <span title="File Path :<?php echo Yii::app()->baseURL . "/Documents/" . $file[$i]; ?>">
                                    <?php echo $file[$i];?>
                            </span>
                            </div>
                        <?php   } }?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top