我使用文件输入插件上传文件。
<input id="input-id" type="file" accept=".mp3, .mp4" name="file" class="form-control"><br>
<input type="hidden" id="getfilename" name="uploadedfile" value="">
这个jquery:
$("#input-id").fileinput({
maxFileSize: 264000,
uploadUrl: "{{url('/rate/uploadfile')}}",
uploadAsync: true,
uploadExtraData:{'_token':$("#csrf_token").val()},
allowedFileExtensions: ['mp3', 'mp4', 'mpeg', 'flv'],
maxFileCount: 1,
showUpload: true,
dropZoneEnabled: false,
});
控制器:
public function uploadfile()
{
$target_dir = 'C:\xampp\htdocs\larafirst\public\newuploads';
$tmpname = $_FILES["file"]["tmp_name"];
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$target_file= $target_dir.DIRECTORY_SEPARATOR.basename($newfilename);
if(move_uploaded_file($tmpname, $target_file)){
echo json_encode($newfilename);die;
}
else
{
echo json_encode(false);die;
}
}
上传时显示绿色刻度线和100%,但永远不会上传。 else语句总是被执行。它在uploadfile中显示false
。