douchongbang6011 2019-03-05 04:14
浏览 101

获取“进程无法访问该文件,因为它正被另一个进程使用。”单元测试uploade文件时出错

Laravel Version: 5.5

PHP Version: 7.1

Database Driver: mysql

Im currently having a problem with unit testing, basically the problem is in the move() function inside my controller, when that function is running the program say the file is being used my another program. and throw this exception

Could not move the file "C:\Users\JUAN ANTONIO\AppData\Local\Temp\phpED45.tmp" to "uploads/partners\15517582682740.jpg" (rename(C:\Users\JUAN ANTONIO\AppData\Local\Temp\phpED45.tmp,uploads/partners\15517582682740.jpg): The process cannot access the file because it is being used by another process. (code: 32))"

here are the snippet of my code:

PartnerTest.php->testStorePartner()

 /** @test */
public function testStorePartner(){
    Storage::fake('local');
    //1. Create Data
    $data = [
        'name' => 'test',
        'file' => UploadedFile::fake()->image('avatar.jpg')
    ];
    //2. Store Data
    $response = $this->actingAs($this->user)->json('POST', route('PartnersStore'), $data);
    //3. Data Stored In Database
    $this->assertDatabaseHas('partners', ['name' => 'test',]);
    //4. Test response status 302 -> redirect
    $response->assertStatus(302);
}

PartnerController->store(Request $request)

public function store(Request $request)
{
    $this->validate($request, [
        'file' => 'mimes:png,jpeg,jpg,gif|max:3000'
    ]);

    // Start of Upload Files
    $formFileName = "file";
    $fileName = "";
    if ($request->$formFileName != "") {
        $fileName = time() . rand(1111,
                9999) . '.' . $request->file($formFileName)->getClientOriginalExtension();
        $path = $this->getUploadPath();
        $request->file($formFileName)->move($path, $fileName);
    }
    // End of Upload Files

    $Partner = new Partner;
    $Partner->name = $request->name;
    $Partner->file = $fileName;
    $Partner->status = 1;
    $Partner->created_by = Auth::user()->id;
    $Partner->save();

    return redirect('/admin/partners')->with('doneMessage', trans('backLang.addDone'));
}

filesystem.php

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c