简体   繁体   English

图像上传在共享服务器中托管的 Laravel 项目中不起作用

[英]Image Upload is not working in a Laravel Project hosted in a shared server

I have deployed a Laravel project in a shared hosting.我在共享主机中部署了一个 Laravel 项目。 I have changed my .env file and copied all files from the public folder to the main directory and deleted the public folder.我已经更改了我的 .env 文件并将所有文件从公用文件夹复制到主目录并删除了公用文件夹。 Now the problem is, whenever I am trying to upload an image, I am getting an internal server error.现在的问题是,每当我尝试上传图像时,都会出现内部服务器错误。 I suppose the problem is the Image Intervention is not getting the right folder to save the image.我想问题是图像干预没有获得正确的文件夹来保存图像。 I have tried the both ways given below:我已经尝试了下面给出的两种方法:

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;               
}   

and

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;               
}

But None of these is working.但这些都不起作用。 Any possible Solution?任何可能的解决方案?

Use laravel base_path function, so your code will look like this使用 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;               
}

Answer Update答案更新

Issue was fileinfo extension missing or disbaled.问题是文件信息扩展丢失或被取消。

Try This.试试这个。

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;
    }

Create imges inside public directory.创建公共目录中imges。

I am handling it like this:我是这样处理的:

// 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, '.'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM