1

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));
}
Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • Enable the extension. http://stackoverflow.com/questions/3579072/php-fileinfo-is-undefined-function – mario Jul 05 '13 at 02:02
  • Hi Mario, I've just checked my php.ini file and it is enabled already. – Jonnny Jul 05 '13 at 02:14
  • Error message is clear `File or path not found`, so extension is loaded, but file does not exists. –  Jul 05 '13 at 07:40
  • 2
    Possible duplicate of [PHP fileinfo is undefined function](https://stackoverflow.com/questions/3579072/php-fileinfo-is-undefined-function) – Waqleh Nov 22 '17 at 10:12

2 Answers2

2

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 ...

acorncom
  • 5,975
  • 1
  • 19
  • 31
  • The file is uploaded to the correct folder ok, It just errors and doesn't save the record in the database. I did change the mimeType back to just 'types' and the same thing happened, file got uplaoded correctly, but this time no error, but no record was saved to the DB. – Jonnny Jul 05 '13 at 20:54
  • 1
    So is the issue that your record isn't showing up in the DB or that the file isn't getting saved or both? I think it would be cleaner to work out your DB saving issues and then we can work on this. – acorncom Jul 06 '13 at 00:03
  • After some playing around, just trial and error, changing the Model to be 'on' => 'insert' seemed to work in that it saved the record. The file was always able to be uploaded, but the record just wouldn't save. Now I can get it to save the record in the DB and also in the upload folder. I've only managed to get the text files and .doc files to save. Currently, .docx file won't save as Yii states they aren't acceptable mime-types. So I see what your saying now about it being temperamental. I'm still using the 'mimeType' key in the rules not the 'types' key. - Jonnny – Jonnny Jul 06 '13 at 04:14
  • Yep. I've found that you have to add extensions to your magic.mgc file to support docx files, depending on your PHP version and base OS. How about you do some research and then come back if you need help ... – acorncom Jul 06 '13 at 14:45
1

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.

Jonnny
  • 4,939
  • 11
  • 63
  • 93