Laravel、jQuery 文件上传错误 - "Call to a member function store() on array"
Laravel, jQuery File upload error - "Call to a member function store() on array"
我用这个库上传文件https://github.com/blueimp/jQuery-File-Upload
当我尝试上传文件时出现错误:"Call to a member function store() on array"
$request->files
变量似乎有问题。当 is 被传递给 foreach 循环时,它没有 return 正确的索引。
感谢您的帮助。
控制器
public function upload(Request $request) {
foreach($request->files as $file) {
$filename = $file->store('library');
}
}
dd($request->files)
的输出
FileBag {#68
#parameters: array:1 [
"files" => array:1 [
0 => UploadedFile {#45
-test: false
-originalName: "img_(17).jpg"
-mimeType: "image/jpeg"
-size: 1260662
-error: 0
path: "D:\xampp\tmp"
filename: "phpD7B1.tmp"
basename: "phpD7B1.tmp"
pathname: "D:\xampp\tmp\phpD7B1.tmp"
extension: "tmp"
realPath: "D:\xampp\tmp\phpD7B1.tmp"
aTime: 2018-05-30 16:10:02
mTime: 2018-05-30 16:10:02
cTime: 2018-05-30 16:10:02
inode: 0
size: 1260662
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "D:\xampp\tmp\phpD7B1.tmp"
}
]
]
}
响应,当我在 foreach 循环中的控制器中使用 var_dump($file) 时。
array(1) {
[0]=>
object(Symfony\Component\HttpFoundation\File\UploadedFile)#45 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(12) "zdjęcie.jpg"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(10) "image/jpeg"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(7445)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(24) "D:\xampp\tmp\php7175.tmp"
["fileName":"SplFileInfo":private]=>
string(11) "php7175.tmp"
}
}
查看
<div id="dropZone">
<h1>Drag & Drop Files...</h1>
<input type="file" id="fileupload" name="files[]" multiple>
</div>
<h1 id="error"></h1><br><br>
<h1 id="progress"></h1><br><br>
<div id="files"></div>
<script>
$(function () {
var files = $("#files");
$("#fileupload").fileupload({
url: '{{ route("libraryUpload") }}',
dropZone: '#dropZone',
dataType: 'json',
autoUpload: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}).on('fileuploadadd', function (e, data) {
var fileTypeAllowed = /.\.(gif|jpg|png|jpeg)$/i;
var fileName = data.originalFiles[0]['name'];
var fileSize = data.originalFiles[0]['size'];
if (!fileTypeAllowed.test(fileName))
$("#error").html('Only images are allowed!');
else if (fileSize > 5000000)
$("#error").html('Your file is too big! Max allowed size is: 5MB');
else {
$("#error").html("");
data.submit();
}
}).on('fileuploaddone', function(e, data) {
var status = data.jqXHR.responseJSON.status;
var msg = data.jqXHR.responseJSON.msg;
if (status == 1) {
var path = data.jqXHR.responseJSON.path;
$("#files").fadeIn().append('<p><img style="width: 100px; height: 100px;" src="'+path+'" /></p>');
} else
$("#error").html(msg);
}).on('fileuploadprogressall', function(e,data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$("#progress").html("Completed: " + progress + "%");
});
});
</script>
尝试在 foreach 循环中 var_dump '$file' 以便您可以了解它的类型
我用这个库上传文件https://github.com/blueimp/jQuery-File-Upload
当我尝试上传文件时出现错误:"Call to a member function store() on array"
$request->files
变量似乎有问题。当 is 被传递给 foreach 循环时,它没有 return 正确的索引。
感谢您的帮助。
控制器
public function upload(Request $request) {
foreach($request->files as $file) {
$filename = $file->store('library');
}
}
dd($request->files)
FileBag {#68
#parameters: array:1 [
"files" => array:1 [
0 => UploadedFile {#45
-test: false
-originalName: "img_(17).jpg"
-mimeType: "image/jpeg"
-size: 1260662
-error: 0
path: "D:\xampp\tmp"
filename: "phpD7B1.tmp"
basename: "phpD7B1.tmp"
pathname: "D:\xampp\tmp\phpD7B1.tmp"
extension: "tmp"
realPath: "D:\xampp\tmp\phpD7B1.tmp"
aTime: 2018-05-30 16:10:02
mTime: 2018-05-30 16:10:02
cTime: 2018-05-30 16:10:02
inode: 0
size: 1260662
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "D:\xampp\tmp\phpD7B1.tmp"
}
]
]
}
响应,当我在 foreach 循环中的控制器中使用 var_dump($file) 时。
array(1) {
[0]=>
object(Symfony\Component\HttpFoundation\File\UploadedFile)#45 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(12) "zdjęcie.jpg"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(10) "image/jpeg"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(7445)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(24) "D:\xampp\tmp\php7175.tmp"
["fileName":"SplFileInfo":private]=>
string(11) "php7175.tmp"
}
}
查看
<div id="dropZone">
<h1>Drag & Drop Files...</h1>
<input type="file" id="fileupload" name="files[]" multiple>
</div>
<h1 id="error"></h1><br><br>
<h1 id="progress"></h1><br><br>
<div id="files"></div>
<script>
$(function () {
var files = $("#files");
$("#fileupload").fileupload({
url: '{{ route("libraryUpload") }}',
dropZone: '#dropZone',
dataType: 'json',
autoUpload: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}).on('fileuploadadd', function (e, data) {
var fileTypeAllowed = /.\.(gif|jpg|png|jpeg)$/i;
var fileName = data.originalFiles[0]['name'];
var fileSize = data.originalFiles[0]['size'];
if (!fileTypeAllowed.test(fileName))
$("#error").html('Only images are allowed!');
else if (fileSize > 5000000)
$("#error").html('Your file is too big! Max allowed size is: 5MB');
else {
$("#error").html("");
data.submit();
}
}).on('fileuploaddone', function(e, data) {
var status = data.jqXHR.responseJSON.status;
var msg = data.jqXHR.responseJSON.msg;
if (status == 1) {
var path = data.jqXHR.responseJSON.path;
$("#files").fadeIn().append('<p><img style="width: 100px; height: 100px;" src="'+path+'" /></p>');
} else
$("#error").html(msg);
}).on('fileuploadprogressall', function(e,data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$("#progress").html("Completed: " + progress + "%");
});
});
</script>
尝试在 foreach 循环中 var_dump '$file' 以便您可以了解它的类型