File Upload

Hi all … I have very annoying problem. I have a table in database with just three fields:

TABLE : tbl_videos

id_vdo (Primary Key)

name_vdo varchar

file_vdo varchar

The problem is that when I insert record with File Vdo field set to "textField" everything works fine. As soon as I change it to file, the create form does not insert any record, neither it shows any error.

I changed following things:

views/videos/_form.php

‘htmlOptions’=>array(‘enctype’=>‘multipart/form-data’)

models/Videos.php

array(‘file_vdo’, ‘file’, ‘types’=>‘flv,mov,mp4’)

controllers/VideosController.php

Although I used proper file upload code but for testing, I manually associate a dummy file name like this:

$model->file_vdo = "test";

Any suggestion, what I am missing or doing wrong???

Thanks to all

I just noticed one thing. I put die() inside "IF" condition like this:

if(isset($_POST[‘Videos’]))

{

die("Testing");

}

I see that after submitting the form, the control does not pass the "IF" condition. What the problem could be ?

Please help!

Thanks

No code. No help. ;)

Controller Creation Action


/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Videos;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

			

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

			$model->file_vdo = "test";

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}

Model Rules:


/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('name_vdo, file_vdo', 'required'),

			array('name_vdo', 'length', 'max'=>255),

			array('file_vdo', 'file', 'types'=>'flv,mov,mp4'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id_vdo, name_vdo, file_vdo', 'safe', 'on'=>'search'),

		);

	}

VIEW _form.php


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'videos-form',

	'enableAjaxValidation'=>false,

	'htmlOptions'=>array(

		'enctype'=>'multipart/form-data'

	)

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'name_vdo'); ?>

		<?php echo $form->textField($model,'name_vdo',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'name_vdo'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'file_vdo'); ?>

		<?php echo $form->fileField($model,'file_vdo'); ?>

		<?php echo $form->error($model,'file_vdo'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>

One more thing to mention … ::)

If I submit the form without selecting any file, it works fine. Problem occured only when you select some file and then submit the form.

BTW jacmoe, I have added code!

Any suggestion ?

Thanks for the code! :)

I bet the problem is that the ‘file’ validator really expects it to be a valid file.

So you should be creating a CUploadedFile from the file in the form.

And thus the create action becomes:


        public function actionCreate()

        {

                $model=new Videos;


                // Uncomment the following line if AJAX validation is needed

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


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

                {

                        

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

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

                   	$tmpPicture = Yii::getPathOfAlias('webroot.uploaded') . '/' .  $picture_name;

                   	$picture_file->saveAs($tmpPicture);

                   	$model->file = $the_file;

                    	if($model->save())

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

                }


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

                        'model'=>$model,

                ));

        } 

Thank you so much jacmoe. But the problem is still there. I have already tried this:


public function actionCreate()

	{

		$model=new Videos;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

			

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

			

			$VideoFile = CUploadedFile::getInstance($model, 'file_vdo');

			

			$VideoFile->saveAs(Yii::app()->basePath.'/../uploads/files/videos/currentvideo.mp4');

                        $model->file_vdo = $VideoFile;

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}

The problem is that the program control does get past the “if(isset($_POST[‘Videos’]))” condition. I have the exact similar table and its MVC structure and it works 100% fine. Not sure where is the problem.

Thanks

Yes, but are you testing against a real uploaded file?

Test for null - or use a die($VideoFile) to see if it’s valid.

The ‘file’ validator will fail if it’s not a valid file - that’s what it’s there for.

Try setting it to something else if you are testing with a dummy string.

I tried that too, even by copying the working code. I have a similar form for images and that works 100% perfect. I am not sure what the hell is with this.

The real problem is that it does get the "IF" condition passed. I tried one thing. I copied the form code from the source view and put it in a temporary PHP file. I replaced the action attribute value to post form to the current page for testing. The code is below:


<?php

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

		die("<h2>Hello</h2>");

?>

<form enctype="multipart/form-data" id="videos-form" action="" method="post">

  <p class="note">Fields with <span class="required">*</span> are required.</p>


     <div class="row">

	<label for="Videos_name_vdo" class="required">Name Vdo <span class="required">*</span></lable>

        <input size="60" maxlength="255" name="Videos[name_vdo]" id="Videos_name_vdo" type="text" />

     </div>


     <div class="row">

	<label for="Videos_file_vdo" class="required">File Vdo <span class="required">*</span></label>		

	<input id="ytVideos_file_vdo" type="hidden" value="" name="Videos[file_vdo]" />

	<input name="Videos[file_vdo]" id="Videos_file_vdo" type="file" />

     </div>


     <div class="row buttons">

	<input type="submit" name="yt0" value="Create" />

     </div>


</form>

Lets suppose you save the above code in temp.php. Now visit this file. You’ll notice that if you submit the form without selecting any File, it works 100% fine. However if you submit the form by selecting some file, it behaves the same way as my YII application.

It looks like we are getting close to the solution :rolleyes:

Jacmoe, while working on the issue, I found that form works perfect if I set file types to JPG or PNG. However when I set file types to "flv, mp4", it stop working.

Your suggestions please!

Thanks

I am not sure if its case sensitive?

Try FLV and MP4 too.

I finally got the problem but unable to know the reason :angry: . It simply does not accept flv and mp4 type files. Do you think it is related to my server configuration? If yes, please suggest some possible solutions. I tried by uploading mp4 type files with simple html form. I put the PHP code to die() when it sees the POST request. Even this simple form behave just like my Yii applicatin!!!

So in simple words, a form containing file field, does not support/handle mp4 and flv type files, atleast on my local server.

Does anyone ever see such problem? I wasted alot of hours on this.

Thanks

What kind of server are you using?

Apache?

I know almost nothing about server configuration, but maybe you need to set some allowed MIME options?

Google will probably help.

Do post the solution here if you find any! :)

Your problem might be happening because of the defined mime types in the /framework/utils/mimeTypes.php file.

Basically this file returns an array (key is the extension and the value is the mime type) and this array is searched later by YII to see if the file is valid.

However, some files might have multiple valid mime types, for example .tgz files can have application/x-tar but also application/x-gzip-compressed, zip files can have ‘application/x-zip’, ‘application/zip’, ‘application/x-zip-compressed’ and so on .

I know that the flash files might have distinct mime types, as specified here http://osflash.org/flv , so you might want to take a look there, and here http://www.feedforall.com/mime-types.htm and depending on this, you might find an answer to your question.

FYI: i will fill a feature request so that this file will be moved to the /config/ directory and to have the possibility to return an array of valid mime types.

Hi twisted, thanks for the reply. Can you please do a quick test? I attached very simple File upload form. It has no File Upload handler. The php code will simply call "die()" if it sees that form was submitted.

Try uploading FLV or MP4 file and please post here what you saw?

1967

Fileupload-test.php

Thanks

Hi guy. I got same situation.

Check your php.ini for "upload_max_filesize" and "post_max_size", It may help.