【问题标题】:Laravel uploading image fails in some images but not in othersLaravel 在某些图像中上传图像失败,但在其他图像中没有
【发布时间】:2021-08-27 20:10:49
【问题描述】:

嘿,昨天我终于把我的网站上传到了一个主机上,我跟着一个视频上传了它。所以我在 public_html 之外创建了一个单独的文件夹来保存不属于 public 文件夹的文件,并且在 public_html 上我从 public 文件夹中删除了文件!现在,当我尝试上传图片时,总是出现错误“照片上传失败。”,但这只发生在某些图片上,而不是全部,我不知道为什么!我试图禁用验证,但给了我同样的错误,所以我不认为这进入验证反正。

来自一个控制器的我的控制器代码:

    public function store(ParceiroPost $request)
{
    $validated_data = $request->validated();

    $newParceiro = new Parceiros();
    $newParceiro->nome = $validated_data['nome'];
    $newParceiro->url_parceiro = $validated_data['link'];


    if ($request->hasFile('foto')) {

        $image = $request->file('foto');
        $input['imagename'] = time().'_'.$newParceiro->nome.'.png';
     
        $destinationPath = public_path('storage/parceiros');
        $img = Image::make($image->path());
        $img->resize(1280, 868)->save($destinationPath.'/'.$input['imagename']);

        $newParceiro->foto_url = $input['imagename'];
        
    }

    $newParceiro->save();

    return redirect()->route('admin.parceiros')
        ->with('alert-msg', 'Parceiro " '. $request->nome .' " foi criado com sucesso!')
        ->with('alert-type', 'success');
}

发生在这里,我以不同的方式保存它:

    public function store(StaffPost $request)
{

    $validated_data = $request->validated();

    $newUser = new User;
    $newUser->email = $validated_data['email'];
    $newUser->name = $validated_data['nome'];
    $newUser->tipo = $validated_data['tipo'];
    $newUser->password = Hash::make($validated_data['password']);


    if ($request->hasFile('foto')) {
        $path = $request->foto->store('fotos_perfil');
        $newUser->foto_url = basename($path);
    }

    $newUser->save();
    
    $newUser = User::where('email', $validated_data['email'])->first();
    //dd($newUser);
    $newUser->markEmailAsVerified();

    return redirect()->route('admin.staff')
        ->with('alert-msg', 'Staff " '. $newUser->name .' " foi criado com sucesso!')
        ->with('alert-type', 'success');
}

验证:

    public function rules()
{
    if(request()->isMethod('put')) // could be patch as well
    {
        return [
            'nome' =>                'required',         
            'foto' =>                'image|max:8192',   // Máximum size = 8Mb
            'link' =>                'nullable',   // Máximum size = 8Mb
        ];
    }
    else
    {
        return [
            'nome' =>                'required',         
            'foto' =>                'image|max:8192|required',   // Máximum size = 8Mb
            'link' =>                'nullable',
        ];
    }
}

我认为这必须与 php 有关,因为如果我注释所有代码并让返回,无论如何都会抛出错误。

这只是发生在一些图像上,我不知道为什么,因为当我在本地主机上时,我可以上传这些图像,但是当我在网站上时,我不能

解决方案:

这是我第一次托管一个网站,所以我不知道文件 php.ini 中存在一些限制上传大小的变量!为此,我更改了这些变量的值,以便上传更大的文件。

【问题讨论】:

  • 您在存储文件夹路径中存储图像时出错?因为你定义了 $destinationPath = public_path('storage/parceiros');像这样。所以请确认您要存储图像的位置。公共路径还是存储路径?
  • 你遇到了什么错误?
  • 请告诉我您想在哪里存储图像...存储路径还是公共路径?所以我可以更好地帮助你
  • @HarshPatel 不,因为我在公共文件夹上有一个指向存储路径的超链接,这发生在另一个控制器上,我以不同的方式保存
  • 您的 php.ini 或 nginx/appache 的上传最大大小是否可能过低?通常这个设置为2MB,如果你上传的图片太大的话apache/nginx或者php会失败而不是你的程序。

标签: php laravel


【解决方案1】:

你的错误是你没有给出完整的路径...试试 "storage_path($destinationPath)"

我分享我的示例代码,如果你有任何错误告诉我...我会更新我的答案

如果您在我的代码下方使用 store 或 storeAs 方法,您将需要在控制器中加载类...

使用 Illuminate\Support\Facades\Storage;

然后我函数你的图片上传代码会是这样的

if($request->hasFile('profileimg')){
      $old_profile_photo = $user->profile_photo_path;
      Storage::disk('public')->delete($old_profile_photo);
      $file = $request->File('profileimg');
      $original_name = $file->getClientOriginalName();  
      $file_ext = $file->getClientOriginalExtension();  
      $destinationPath = 'app/public/users';
      $file_name = "user".time().uniqid().".".$file_ext;
      $file->move(storage_path($destinationPath),$file_name); //save at storage path
      $user->profile_photo_path = "users/".$file_name;
      /*$resize_image = Image::make($file->getRealPath()); //for Resize the Image
      $resize_image->resize(150, 150, function($constraint){ //resize with 150 x 150 ratio
      $constraint->aspectRatio();
      })->save(storage_path($destinationPath) . '/' . $file_name); */
      // $path = $request->profile_img->store('uploads'); 
      // $path = $file->storeAs('users', $file_name, 'public');
      // $user->profile_photo_path = $path; //path store in object variable
     $user->save();
  }

【讨论】:

  • 感谢您的帮助,但问题是 @Aless55 所说的 php 限制。
  • 兄弟假设你有另一个上传图片失败,但还有另一个关于路径的错误,所以我分享......因为当我是 laravel 的新手时,我遇到了关于图片上传的同样问题,在本地它可以完美运行,但在服务器中由于路径问题而无法完美运行
  • 是的,但我不需要使用您代码中的任何内容!但是感谢您尝试帮助我
  • @GonçaloBastos 您的具体问题是什么? &请分享您的问题的解决方案。请把它放在答案中,以便对其他人有所帮助
猜你喜欢
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多