制作一个api以上传带有音频的多文件,一切正常,但是音频文件无法上传 然后用dd($ request)-> all上传 然后它起作用 但是在以任何条件上传时,它在每个clientoriginalName,扩展名, 如何解决这个问题...
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'nullable',
'file' => 'required|file|mimes:' . File::getAllExtensions() . '|max:' . File::getMaxSize(),
'Fileaudio' =>'nullable|mimes:audio/mpeg,mpga,mp3,wav,aac'
]);
//////////// All files //////////////////
$file = new File();
$title = $request->title;
$uploaded_file = $request->file('file');
$filename = $uploaded_file->getClientOriginalName();
$original_ext = $uploaded_file->getClientOriginalExtension();
$type = $file->getType($original_ext);
$filepath = $uploaded_file->storeAs('public/upload/files/',$filename);
$files = URL::asset('storage/upload/files/' . $filename);
$description = $request->description;
$user_id = Auth::user()->id;
/////////// Audio at null /////////////////
$Fileaudio = $request->file('audio');
$audioname = $Fileaudio->getClientOriginalName();
$audiopath =$Fileaudio->storeAs('public/upload/files/audio/', $audioname);
//return $audiopath;
dd($request->all());
}
}
我正在向邮递员发送请求...
答案 0 :(得分:1)
在storage / app / public和'upload/files'
内创建一个文件夹/upload/files/audio
然后运行命令: php artisan storage:link
此命令会将您的存储文件夹链接到公用文件夹
更新您的代码:
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
public function store(Request $request)
{
// validation
$this->validate($request, [
'title' => 'required',
'description' => 'nullable',
'file' => 'required|file|mimes:jpeg,jpg,png,gif|max:2048',
'audio' =>'nullable|file|mimes:audio/mpeg,mpga,mp3,wav,aac'
]);
// code for upload 'file'
if($request->hasFile('file')){
$uniqueid=uniqid();
$original_name=$request->file('file')->getClientOriginalName();
$size=$request->file('file')->getSize();
$extension=$request->file('file')->getClientOriginalExtension();
$name=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
$imagepath=url('/storage/uploads/files/'.$name);
$path=$request->file('file')->storeAs('public/uploads/files/',$name);
}
// code for upload 'audio'
// handle multiple files
if(is_array($request->file('audio')))
{
$audios=array();
foreach($request->file('audio') as $file) {
$uniqueid=uniqid();
$original_name=$file->getClientOriginalName();
$size=$file->getSize();
$extension=$file->getClientOriginalExtension();
$filename=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
$audiopath=url('/storage/upload/files/audio/'.$filename);
$path=$file->storeAs('/upload/files/audio',$filename);
array_push($audios,$audiopath);
}
$all_audios=implode(",",$audios);
}else{
// handle single file
if($request->hasFile('audio')){
$uniqueid=uniqid();
$original_name=$request->file('audio')->getClientOriginalName();
$size=$request->file('audio')->getSize();
$extension=$request->file('audio')->getClientOriginalExtension();
$filename=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
$audiopath=url('/storage/upload/files/audio/'.$filename);
$path=$file->storeAs('public/upload/files/audio/',$filename);
$all_audios=$audiopath;
}
}
}
在您的邮递员请求中:
add key : "file" for image file ,
"audio" for audio file
答案 1 :(得分:0)
您可以使用这些句子来上传任何分段内容
$file = $request->file;
$filename = time() . '.' . $file->getClientOriginalExtension();
$file->move('your-path', $filename);
并且如果您需要上传多音频或图像,请确保邮递员的钥匙是这样写的
images[]
or
audios[]