【问题标题】:Image Upload is not working in a Laravel Project hosted in a shared server图片上传在共享服务器中托管的 Laravel 项目中不起作用
【发布时间】:2018-02-15 10:09:42
【问题描述】:

我在共享主机中部署了一个 Laravel 项目。我已经更改了我的 .env 文件并将所有文件从公用文件夹复制到主目录并删除了公用文件夹。现在的问题是,每当我尝试上传图像时,都会出现内部服务器错误。我想问题是图像干预没有得到正确的文件夹来保存图像。我尝试了以下两种方法:

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = public_path('/images/admin/' . $filename);
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}   

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = '/images/admin/' . $filename;
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}

但是这些都不起作用。任何可能的解决方案?

【问题讨论】:

  • 我已将公共路径更改为基本路径,并且必须安装 php fileinfo 才能让干预图像在 Live 上运行。谢谢大家的回答。

标签: php laravel-5


【解决方案1】:

使用 laravel base_path 函数,所以你的代码会是这样的

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = base_path().'/images/admin/' . $filename;
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}

答案更新

问题是文件信息扩展丢失或禁用。

【讨论】:

  • 你的图片文件夹有写权限吗?
  • 我有权限 0755
  • 你能告诉我你的错误日志吗?还是您得到的确切例外?
  • 糟糕,好像出了点问题。当我尝试上传图像时,我得到了这个。错误日志中没有任何内容
  • 很可能问题出在您的 .env 文件上,如果您希望 laravel 为您显示错误而不是通用消息“哎呀”消息,请将 .env.example 复制到 .env。然后它将显示详细的错误消息。
【解决方案2】:

试试这个。

use Storage;
use File;
if(!empty($request->file('admin_pro_pic')))
    {
        $file = $request->file('admin_pro_pic') ;
        $fileName = $file->getClientOriginalName() ;
        $destinationPath = public_path().'/images/' ;
        $file->move($destinationPath,$fileName);
       
        $admin->image=$fileName;
    }

在公共目录中创建 imges

【讨论】:

    【解决方案3】:

    我是这样处理的:

    // check for defined upload folder inside .env file, otherwise use 'public'
    $publicUploadDir = env('UPLOAD_PUBLIC', 'public/');
    // get file from request
    $image = $request->file('admin_pro_pic');
    // hasing is not necessary, but recommended
    $new['path'] = hash('sha256', time());
    $new['folder] = 'images/admin/';
    $new['extension'] = $file->extension();
    // store uploaded file and retrieve path
    $image->storeAs($publicUploadDir, implode($new, '.'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2020-04-01
      • 1970-01-01
      相关资源
      最近更新 更多