laravel 在字符串上调用成员函数 move()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44647180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:10:27 来源:igfitidea点击:
Call to a member function move() on string
提问by Tovo
Kindly assist me with the code below. I keep getting this error "Call to a member function move() on string" when am trying to upload multiple images.Thanks in advance.
请帮助我使用下面的代码。当我尝试上传多个图像时,我不断收到此错误“调用字符串上的成员函数 move()”。提前致谢。
This is the code for my view page
这是我查看页面的代码
{!!Form::open(['method'=>'POST','action'=>'ClientController@uploadLand','files'=>true])!!}
{{ csrf_field() }}
<div class="admin-form">
<div class="row">
<div id="maindiv">
<div id="formdiv">
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<!-- <input type="submit" value="Upload File" name="submit" id="upload" class="upload"/> -->
<br/>
<br/>
</div>
</div>
</div>
{!!Form::close()!!}
This the controller code
这是控制器代码
public function uploadLand(Request $request)
{
for($i=0;$i<count($_FILES["file"]["name"]);$i++){
$image= "";
$file_path="images/uploads";
$imageName = $_FILES['file']['name'][$i];
$image->move(public_path($file_path),$imageName);
//$image->move_uploaded_file("images/uploads".$imageName);
}
}
回答by Pankaj Makwana
Try below code :
试试下面的代码:
if ($request->hasFile('file')) {
$destinationPath = 'path/th/save/file/';
$files = $request->file('file'); // will get all files
foreach ($files as $file) {//this statement will loop through all files.
$file_name = $file->getClientOriginalName(); //Get file original name
$file->move($destinationPath , $file_name); // move files to destination folder
}
}