gpt4 book ai didi

php - 音频验证问题 laravel 5.4

转载 作者:行者123 更新时间:2023-12-04 13:45:27 26 4
gpt4 key购买 nike

我正在尝试验证我的表单请求。我正在做的是上传音频,即 mp3、wav 等,我遇到的问题是它一直向我抛出一条错误消息,说“文件类型必须是 mp3”,我尝试上传一张图片,它说文件类型必须是 mp3,我也尝试上传 mp3,它说同样的事情,下面是我的音频 Controller 。

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

class UploadController extends Controller
{
public function uploadsingle(Request $request)
{
$validator = Validator::make($request->all(), [
'song' => 'required|mimes:image/png',
]);

if($validator->fails()){
return redirect()->back()->withErrors($validator)->withInput();
}

///save audio, etc
echo 'validation passed';
}
}

这是我的html代码

<title>Upload page</title>
@include('layouts.page-life')
@include('layouts.navbar')




<h1>Upload Page</h1>
<br>
<form action="{{ route('doupload') }}" method="POST">
{{ csrf_field() }}
@if (count($errors) > 0)
<div class="alert alert-danger alert-dismissible" role="alert">
<ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<input name="song" type="file"/>
<br>
<button class="btn">Submit</button>
</form>

最佳答案

以下验证将接受 wav、mp3 等。这段代码对我有用。

$validator = Validator::make($request->all(), [
'song' => 'required|mimes:application/octet-stream,audio/mpeg,mpga,mp3,wav',
]);

或者写在单独的请求文件中:eg: app/Http/Requests/SongStoreRequest.php

public function rules(){
['song' => 'required|mimes:application/octet-stream,audio/mpeg,mpga,mp3,wav'];
}

关于php - 音频验证问题 laravel 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com