![](/img/trans.png)
[英]Call to a member function getClientOriginalExtension() on string in Laravel
[英]Call to a member function getClientOriginalExtension() on string
在我的laravel控制器中,當我嘗試上傳圖像時,出現此錯誤。
我知道問題出在我的控制器內部
$image = $request['images'];
if ($image)
{
$imgfile = $request['images'];
$imgpath = 'uploads/users/images/'.$user->id;
File::makeDirectory($imgpath, $mode = 0777, true, true);
$imgDestinationPath = $imgpath.'/'; // upload folder in public directory
$ext1 = $imgfile->getClientOriginalExtension();
$filename1 = uniqid('vid_').'.'.$ext1;
$usuccess = $imgfile->move($imgDestinationPath, $filename1);
}
這是我的jQuery代碼,我的表單ID是更新配置文件,我按其名稱獲取所有字段值
$("#updateprofile").on('submit', function(e){
e.preventDefault(e);
var redirect_url = $(this).find("[name='redirect_url']").val();
var url = $(this).attr('action');
var method = $(this).attr('method');
var myData = {
name: $(this).find("[name='name']").val(),
gender:$(this).find("[name='gender']").val(),
lastname:$(this).find("[name='lastname']").val(),
email:$(this).find("[name='email']").val(),
mobile:$(this).find("[name='mobile']").val(),
address:$(this).find("[name='address']").val(),
city: $(this).find("[name='city']").val(),
state:$(this).find("[name='state']").val(),
country:$(this).find("[name='country']").val(),
pin:$(this).find("[name='pin']").val(),
images: document.getElementById('imageToUpload').files[0],
}
console.log("data", myData);
$.ajax({
type: "PUT",
url: url,
dataType: 'JSON',
data: myData,
success: function(data) {
console.log(data);
//alert("Profile update successfully")
//window.location.href = redirect_url;
},
error: function(jqXHR, textStatus, errorThrown) {
alert("das");
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
});
試試這個(假設您表單輸入的id
和name
值為images
),
if($request->hasFile('images')) {
$imgfile = Input::file('images');
$imgpath = 'uploads/users/images/'.$user->id;
File::makeDirectory($imgpath, $mode = 0777, true, true);
$imgDestinationPath = $imgpath.'/';
$ext1 = $imgfile->getClientOriginalExtension();
$filename1 = uniqid('vid_').'.'.$ext1;
$usuccess = $imgfile->move($imgDestinationPath, $filename1);
}
在您的jQuery代碼中:嘗試更改
images: document.getElementById('imageToUpload').files[0],
至
images: document.getElementById('imageToUpload').files[0].value,
在通過表單上傳文件時,您應該在ajax調用中指定contentType: 'multipart/form-data',
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.