Yii2: finfo_file(C:\xampp\tmp\php29C.tmp): 无法打开流:没有那个文件或目录 [英] Yii2: finfo_file(C:\xampp\tmp\php29C.tmp): failed to open stream: No such file or directory

查看:31
本文介绍了Yii2: finfo_file(C:\xampp\tmp\php29C.tmp): 无法打开流:没有那个文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取错误 finfo_file(C:\xampp\tmp\php29C.tmp): 无法打开流:上传多个文件时没有这样的文件或目录.

尝试在 saveAs() 之前插入 $model->save(); 但它只上传一个文件而不是所有文件,也没有得到路径在数据库中对于每个正在上传的文件.

tried inserting $model->save(); before saveAs() but then it upload only one file not all files, also not getting path in database for each file which is getting uploaded.

控制器:

public function actionCreate()
        {
            $model = new RoomTypes();

            if ($model->load(Yii::$app->request->post()))
            {
                $imageName = $model->room_type;
                     $model->file = UploadedFile::getInstances($model, 'file');

                foreach ($model->file as $file_instance) 
                {
                        $model->save();
                        $file_instance->saveAs('uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension);
                        //save the path in the db column
                        $file_instance->$model->images = 'uploads/room_img/'.$imageName.'.'.$file_instance->extension;


                        return $this->redirect(['view', 'id' => $model->id]);
                }
            } 
            else 
            {
                return $this->render('create', 
                    [
                    'model' => $model,
                ]);
            }
        }

推荐答案

可能我已经回答了这个问题 此处 在您发表评论后的最后一次更新中,但我会在这里添加一个不同的答案,因为它是一个除了其他问题之外,还有更多不同的问题.在保存文件后使用 $file_instance->baseName$file_instance->extension 时会抛出此错误.saveAs() 希望它的内容如下:

It is possible that I already have answered this here within the last update after your comments but I'll add a different answer here as it is one more different issue beside the others. This error is thrown when using $file_instance->baseName or $file_instance->extension after saving the file. saveAs() whish its content is the following :

public function saveAs($file, $deleteTempFile = true)
{
    if ($this->error == UPLOAD_ERR_OK) {
        if ($deleteTempFile) {
            return move_uploaded_file($this->tempName, $file);
        } elseif (is_uploaded_file($this->tempName)) {
            return copy($this->tempName, $file);
        }
    }
    return false;
}

有一个布尔值 $deleteTempFile 默认设置为 true.这意味着它会在保存后删除临时文件,您将无法在同一请求中再次保存上传的文件.如果需要,您可以通过执行以下操作将其设置为 false:

has a boolean $deleteTempFile argument set to true by default. which means it will delete the temporary file after saving and you will not be able to save the uploaded file again in the same request. You can set it to false if needed by doing :

$file_instance->saveAs('...',false)

但是在这种情况下这不是一个好习惯,因为您将获得同一文件的 2 个副本.如果文件已经保存到它的新路径,那么为什么不从 tmp 文件夹中删除它.在调用 saveAs 之前,只需将该路径保存在一个变量中() 并在需要时使用它,就像上面链接的答案中所做的那样:

BUT it won't be a good practice in this case as you are getting 2 copies of the same file. If the file is already saved to its new path then why not deleting it from the tmp folder. Just hold that path in a variable before calling saveAs() and use it when needed like it is done in the answer linked above :

$path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;
$file_instance->saveAs($path);
$model->images = $path;

<小时>

您需要解决的其他问题是:


Other issues you need to solve are :

foreach ($model->file as $file_instance) 
{
     $model->save();
     $model->images = $path;
}

这里的 $model->save() 对我来说没有逻辑.您有一个对象的单个实例,并且 foreach 文件实例存储在其属性 $model->file 中,您一次又一次地保存孔模型.我从第二个 ($model->images = $path) 中了解到,您的模型具有一个属性 images,您可以为其分配一个字符串值.但是您在这里有多个图像,其中每个图像都有自己的路径,并且在循环内您每次都使用下一个图像路径字符串覆盖它.如果是这种情况,我可能会存储文件夹的路径,以便稍后可以找到所有这些图像:

The $model->save() here have no logic to me. You have a single instance of an object and foreach file instance stored in its attribute $model->file you are saving the hole model again and again. And I understand from the second one ($model->images = $path) that your model have an attribute images to which you can assigning a string value. but you have multiple images here where each of them has its own path and inside the loop you are overriding it each time with the next image path string. If is the case I would probably store the path to folder where I can find all those images later instead :

$model->images = 'uploads/room_img/[a_collection_id_or_wathever_unique]/'

在该文件夹中,我将托管相关图像.或者,我可能会通过在循环内执行此操作来托管由 ; 或等效物分隔的所有路径:

And inside that folder I would host the related images. Or maybe I'll host all paths separated by ; or equivalent by doing this inside the loop :

$model->images .= $path . ';';

如果 images 是由 ActiveRecord 类表示的相关模型,那么上面链接的答案应该可以解决它.

In case if images are related models represented by an ActiveRecord class then the answer linked above should fix it.

这篇关于Yii2: finfo_file(C:\xampp\tmp\php29C.tmp): 无法打开流:没有那个文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆