在laravel 5.3的Folder中上传文件时出现问题?

时间:2019-08-01 08:09:39

标签: javascript laravel laravel-5

我试图在laravel中上传图片,但是在文件夹中上传图片时出现错误,当我上传图片并单击Submit按钮时,在上传文件时出现了问题,我认为这是错误的线...

 move_uploaded_file($imageName, $moveable_file);

这是我的usercontrolle.php文件

public function dropzone(Request $request)
{
    $user = Auth::user()->toArray();
    $user_id = $user['id'];
    $type = 'photo';
    $type_id=0;
    $data = $_FILES["image"];

    //dd($data);
    $doc_id = $_POST["doc_id"];
    $doc_name = $_POST["doc_name"];
    if($doc_id)
    {   $img_id=$doc_id;
        $img_name=$doc_name;
        $response = $this->userService->deleteDocument($img_id,$img_name,$user_id,$type,$type_id);
    }
  // $image_array_1 = explode(";", $data);
  // $image_array_2 = explode(",", $image_array_1[1]);
  // $data = base64_decode($image_array_2[1]);

  $storage_path = env('DOCUMENT_STORAGE_PATH');
  $profile_upload_dir = str_replace(["/","\\"], [DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR], $storage_path); 

    if($type_id != '0'){
        $destination_path = $profile_upload_dir . $user_id ."\\". $type."\\". $type_id;
        $destination_path = str_replace(["/","\\"], [DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR], $destination_path);   
    }else{
        $destination_path = $profile_upload_dir . $user_id ."\\". $type;
        $destination_path = str_replace(["/","\\"], [DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR], $destination_path);   
    }
        if(!is_dir($destination_path)) {
            mkdir($destination_path, 0777,true);
        }
        $imageName = time() . '.png';
       // dd($imageName);
        $moveable_file = str_replace(["/","\\"], [DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR], $destination_path.'\\'.$imageName); 
        //dd($moveable_file);
        move_uploaded_file($imageName, $moveable_file);
        // file_put_contents($moveable_file, $data);
        //$image_file = addslashes(file_get_contents($moveable_file));
        $user = Auth::user()->toArray();
          //dd($user);
        $user_id = $user['id'];
        $type_id = 0;
        if(isset($photo['type_id']) && !empty($photo['type_id'])){
            $type_id = $photo['type_id'];
        }
        //$photo['file']=$_FILES['photoimg'];
        $photo['type']='photo';
        $result = $this->userService->storeUserDocuments($imageName, $photo['type'], $type_id, $user_id);
        // echo '<img src="data:image/png;base64,'.base64_encode($data).'" data-action="zoom" class="pull-left" style="height: 130px;width:130px;">';

}

1 个答案:

答案 0 :(得分:0)

您还可以使用image intervention上传图像。 首先,使用以下命令将其安装在您的laravel项目中     作曲者需要干预/形象 安装后,打开config / app.php,然后将它们添加到$ providers数组中。

Intervention\Image\ImageServiceProvider::class

此外,将此包的外观添加到$ aliases数组。

'Image' => Intervention\Image\Facades\Image::class

此后,您就可以添加图像了 将此添加到您的控制器

use Intervention\Image\Facades\Image;

这里是如何添加图像的示例示例,请在控制器中使用

//Handle the user upload of avatar
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');

        $filename = time().'.'.$avatar->getClientOriginalExtension();  //use time to create file name
        Image::make($avatar)->resize(300,300)->save( public_path('/images/'.$filename) );


        $user->avatar = $filename;
       //Handle the user upload of avatar
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');

        $filename = time().'.'.$avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300,300)->save( public_path('/images/'.$filename) );


        $user->avatar = $filename;
       // $user->save(); //To save the name of the file in the database
    }
    }