Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.5-多幅图像上传_Php_Laravel 5_Image Uploading - Fatal编程技术网

Php Laravel 5.5-多幅图像上传

Php Laravel 5.5-多幅图像上传,php,laravel-5,image-uploading,Php,Laravel 5,Image Uploading,我的控制器中有以下功能: use File; use Image; //image intervention library ... public function upload(Request $request) { //make sure there is a folder in public with the username $username = Auth::user()->name; $folderpath = publi

我的控制器中有以下功能:

use File;
use Image; //image intervention library
...
public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;

        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '.' . $file->getClientOriginalExtension();
                $path = $folderpath . $filename;
                Image::make($file)->resize(800,400)->save($path);
            endforeach;
        endif;

        return 'success';
    }
只有在我上传多张图片时才保存最后一张图片

我试着:

public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;

        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '.' . $file->getClientOriginalExtension();
                $path = $folderpath . $filename;
                $file->save($path);
            endforeach;
        endif;

        return ''success;
    }
这给我带来了一个错误:

方法save不存在

我目瞪口呆,似乎我并没有用模型来实例化它。但在这种情况下,如果它只是一个直接的文件上传,我如何用模型实例化它呢

laravel
中上传多幅图像的最佳方式是什么

更新

在阅读了@kunal的答案后,我设法解决了这个问题,为文件名添加了一个唯一的数字:

public function upload(Request $request)
    {
        //make sure there is a folder in public with the username
        $username = Auth::user()->name;
        $folderpath = public_path('images/' . $username . '/');
        File::makeDirectory($folderpath, $mode = 0777, true, true);

        $files = $request->file;
        $count = 0;//<-- add a counter
        if(!empty($files)):
            foreach($files as $file):
                $filename = 'post_' . time() . '_' . $count . '.' . $file->getClientOriginalExtension();//<-- add counter to the file name
                $path = $folderpath . $filename;
                Image::make($file)->resize(800,400)->save($path);
                $count ++;//<-- increase the value
            endforeach;
        endif;

        return 'success';
    }
公共功能上传(请求$Request)
{
//确保公用文件夹中有用户名为的文件夹
$username=Auth::user()->name;
$folderpath=public_path('images/'.$username./');
File::makeDirectory($folderpath,$mode=0777,true,true);
$files=$request->file;
$count=0;//getClientOriginalExtension();//调整大小(800400)->保存($path);

$count++;//我想你可能错过了这一部分

<input type="file" id="gallery" name="file[]" />

我想你可能错过了这一部分

<input type="file" id="gallery" name="file[]" />
请尝试以下代码:

$files= Input::file('image');
$destinationPath= 'images';
$images=array(); 
foreach($files as $file){
    $fullname= $file->getClientOriginalName(); 
    $hashname  = $fullname; 
    $upload_success   =$file->move($destinationPath, $hashname);
    $images[]=$fullname;
    $has= implode(",",$images);
}
$modelname= new Modelname;
$modelname->image_attachment    =  $has;  
$modelname->save();
和yout html页面:

<input type="file" id="image" name="image[]" />

尝试以下代码:

$files= Input::file('image');
$destinationPath= 'images';
$images=array(); 
foreach($files as $file){
    $fullname= $file->getClientOriginalName(); 
    $hashname  = $fullname; 
    $upload_success   =$file->move($destinationPath, $hashname);
    $images[]=$fullname;
    $has= implode(",",$images);
}
$modelname= new Modelname;
$modelname->image_attachment    =  $has;  
$modelname->save();
和yout html页面:

<input type="file" id="image" name="image[]" />

您可能正在寻找此类材料:-

if ($request->hasFile('files')) {
$files = $request->file('files');
foreach($files as $file){
    $extension = $file->getClientOriginalExtension();
    $fileName = str_random(5)."-".date('his')."-".str_random(3).".".$extension;
    $folderpath  = 'images'.'/';
    $file->move($folderpath , $fileName);
}
}
<input type="file" id="gallery" name="files[]" multiple />
if($request->hasFile('files')){
$files=$request->file('files');
foreach($files作为$file){
$extension=$file->getClientOriginalExtension();
$fileName=str_random(5)。“-”.date('his')。“-”.str_random(3)。“$extension;
$folderpath='images'。/';
$file->move($folderpath,$fileName);
}
}

您可能正在寻找此类材料:-

if ($request->hasFile('files')) {
$files = $request->file('files');
foreach($files as $file){
    $extension = $file->getClientOriginalExtension();
    $fileName = str_random(5)."-".date('his')."-".str_random(3).".".$extension;
    $folderpath  = 'images'.'/';
    $file->move($folderpath , $fileName);
}
}
<input type="file" id="gallery" name="files[]" multiple />
if($request->hasFile('files')){
$files=$request->file('files');
foreach($files作为$file){
$extension=$file->getClientOriginalExtension();
$fileName=str_random(5)。“-”.date('his')。“-”.str_random(3)。“$extension;
$folderpath='images'。/';
$file->move($folderpath,$fileName);
}
}

没有那么复杂。 从幼虫5.8开始,您可以执行以下操作:

collect($request->images)->each(function ($image) {
    return $image->store('images', 'public');
});

它将图像放在公共磁盘的images文件夹中。

没有那么复杂。 从幼虫5.8开始,您可以执行以下操作:

collect($request->images)->each(function ($image) {
    return $image->store('images', 'public');
});

它将图像放在公共磁盘的images文件夹中。

你想为单个用户多个图像吗?没有重复id吗?你想为单个用户多个图像吗?没有重复id吗?OP什么时候添加了html代码?你怎么知道OP错过了它?我猜想,他提到的
行只保存了最后一个图像
@arunkumar你的答案这不是我的问题,但我认为是有价值的,因为像我这样的初学者总是会错过一些错误。是的,明白了!OP是什么时候添加html代码的?你怎么知道OP错过了它?我假设,根据他提到的只保存最后一张图片的
@arunkumar你的答案不是我的问题,但我认为是有价值的,因为它是一些错误像我这样的初学者总是会错过。是的,明白了!你是怎么做到的?make($file)->resize(800400);用你的代码?我正试图用这种方法来保存resize图像foreach($files as$file){$fullname=time()。$file->getClientOriginalExtension();$path=public_path($images/。$imageName);Image::make($Image->getRealPath())->resize(800400)->save($path);$images[]=$fullname;$has=内爆(“,”,$images);}你是怎么做的?make($file)->resize(800400);使用你的代码?我正试图以这种方式保存resize imagesforeach($file){$fullname=time()。$file->getClientOriginalExtension();$path=public_path('images/'.$imageName);Image::make($Image->getRealPath())->resize(800400)->save($path);$images[]=$fullname;$has=内爆(“,”,$images);}你的代码不是我想要的,但是
str\u random(5)
确实解决了我的问题。我只保留了我自己的代码,然后将
$count
添加到
$filename
$count++;
循环中,我的问题就解决了。请检查我的更新。@sooon-hehehh很高兴帮助我的stru-random帮助你:py你的代码并不完全是我想要的,但是
stru-random(5)
确实解决了我的问题。我只保留了我自己的代码,然后将
$count
添加到
$filename
$count++;
循环中,我的问题就解决了。请检查我的更新。@soon-hehehh很高兴帮助我的stru\u随机帮助你:P