【问题标题】:Image upload on laravel doesn't work online : Laravellaravel 上的图片上传无法在线工作:Laravel
【发布时间】:2021-05-25 06:52:04
【问题描述】:

该项目在我的本地主机上运行良好,但在实时共享服务器上出现问题。

  1. 我已尝试将此代码添加到我的 index.php 中
// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});
  1. 我已尝试将此代码添加到我的公共文件夹中的新 sym.php 文件中

<?php 

$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';


?>
  1. 我尝试在我的 web.php 中添加它,然后运行 ​​site/linkstorage
Route::get('/linkstorage', function () {
    Artisan::call('storage:link');
});

这些解决方案都不起作用

这是我的控制器代码的 sn-p:


    public function storeBrand(Request $request){
            $this->validate($request, ['brand_name'=> 'required',
                'brand_url'=> 'required',
                'brand_image'=>'image|nullable|max:1999']);
    
    
    
            if($request->hasFile('brand_image')){
                //1 : get filename with ext
                $fileNameWithExt = $request->file('brand_image')->getClientOriginalName();
    
                //2 : get just file name
                $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
    
                //3 : get just extension
                $extension = $request->file('brand_image')->getClientOriginalExtension();
    
                //4 : file name to store
    
    
                $fileNameToStore = $fileName.'_'.time().'.'.$extension;
    
                //upload image
    
                $path =$request->file('brand_image')->storeAs('public/BrandImages', $fileNameToStore);
    
            }
    
            else{  
    
                $fileNameToStore ='noimage.jpg';
    
            }
    
            $brand=new Brand();
            $brand->brand_name =$request->input('brand_name');
            $brand->brand_url =$request->input('brand_url');
            
    
            $brand->brand_image =$fileNameToStore;
    
            $brand->save();
    
            return redirect('/create_brand')->with('status', 'The '.$brand->brand_name.' Brand has been saved successfully. Create another one.');

注意

上传图片时可以追踪路径,但是找不到图片,返回空图片。

感谢您的时间和帮助。

【问题讨论】:

标签: php laravel


【解决方案1】:

我有解决办法:

我在我的 web.php 路由文件中写下了这段代码:

Route::get('/linkstorage', function () { $targetFolder = base_path().'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage'; symlink($targetFolder, $linkFolder); });

之后我导航到我的 url/linkstorage 成功了!!

【讨论】:

    【解决方案2】:

    查看控制器中的代码,似乎是正确的。可能错误出现在与此方法关联的 Blade 文件中的表单中。根据经验,我倾向于忘记写这个,这可能会解决你的错误。

    把这个写在表单标签上。

    <form action="{{ insert the route here }}" method="POST" enctype="multipart/form-data">
       // insert form input fields here...
    </form>
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2021-02-03
      • 2020-03-21
      • 2014-12-22
      • 2019-11-13
      • 2020-10-03
      • 2018-04-17
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多