Jonnny
Jonnny

Reputation: 5039

Yii finfo_file() not found

I'm using Yii to upload some files. And I'm getting the following error.

finfo_file() [<a href='function.finfo-file'>function.finfo-file</a>]: File or path not found 'C:\xampp\tmp\phpF48A.tmp'

I'm not sure exactly what it's from i've read around on the topic, I'm not sure I have the magic.mime database, do I need to get this and configure it? Or is the problem due to something else. This is the first time I've used Yii, so i'm not sure if it's just a config problem or what. This is the stack:

 if($this->mimeTypes!==null)
221         {
222             if(function_exists('finfo_open'))
223             {
224                 $mimeType=false;
225                 if($info=finfo_open(defined('FILEINFO_MIME_TYPE') ?      FILEINFO_MIME_TYPE : FILEINFO_MIME))
226                     $mimeType=finfo_file($info,$file->getTempName());
227             }

Any help, greatly appreciated it. To add, the file is saved in the correct directory, it records aren't saved in the DB though.

Thanks

Jonny

Updated code

Model

array('file', 'file', 'allowEmpty' => false, 'maxSize'=> 512000, 'maxFiles'=> 1, 
        'mimeTypes' => 'application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/rtf, text/plain',

Controller (Pretty much the default Gii code)

    public function actionCreate() {

    $model = new File;


    if (isset($_POST['File'])) {
        $model->setAttributes($_POST['File']);

        // Set file
        $model->file = CUploadedFile::getInstance($model,'file');

        // Set directory
        $dest = Yii::getPathOfAlias('application.uploads');

        $model->tmp_name = time();

            $model->file->saveAs(($dest . '/' . $model->tmp_name . '.' . $model->file->extensionName));


        if ($model->save()) {


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

    $this->render('create', array( 'model' => $model));
}

Upvotes: 1

Views: 1057

Answers (2)

Jonnny
Jonnny

Reputation: 5039

I'm not really sure why or what was wrong with this. All I can show is the final code that seems to be working fine (with the exception that i'm yet to sort the .docx uploading. All other types upload fine though).

Model:

            array('file', 'file', 'on' => 'insert', 'maxSize'=> 512000, 'maxFiles'=> 1, 
        'mimeTypes' => 'application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/rtf, text/plain',
        'tooLarge'=> 'File cannot be larger than 500KB.',
        'wrongType'=> 'Format must be:<code>.doc</code>, <code>.docx</code>, <code>.txt</code>, <code>.rtf</code>'),

Controller:

    public function actionCreate() {

    $model = new File;


    if (isset($_POST['File'])) {

        $model->setAttributes($_POST['File']);

        // Set file
        $model->file = CUploadedFile::getInstance($model,'file');

        // Set directory
        $dest = Yii::getPathOfAlias('application.uploads');

        $model->tmp_name = time();
        $model->file_type = $model->file->type;
        $model->file_size = $model->file->size;


        if ($model->save()) {

            $model->file->saveAs(($dest . '/' . $model->tmp_name . '.' . $model->file->extensionName));

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

        }
    }

    $this->render('create', array( 'model' => $model));
}

Hope that's some help to others.

Upvotes: 1

acorncom
acorncom

Reputation: 5955

I've found the mimeType checking portion of Yii's file validation system to be a bit problematic (or at least temperamental ;-). There are a number of glitches you can run into, but this is different than the errors I've seen before.

Per @PeterM's comment, it does look like your temp file is unable to be found, meaning that's going to be difficult to finish validating it. If you disable the mimeType validation and then rename the file as part of uploading it (so that you know where it should be), does your file end up getting uploaded?

It's probably best to make sure your upload system is quite solid and then work on adding in the mimeType validations. Because they can be a bit picky ...

Upvotes: 2

Related Questions