【问题标题】:Laravel Storage files get damaged while uploadingLaravel 存储文件在上传时损坏
【发布时间】:2016-05-12 11:54:30
【问题描述】:

所以我一直在我的项目中使用 ftp 函数(manually setting $conn_id, making fpt_put($conn_id,...), conn_close etc)

现在我在我的控制器设置主机用户名密码中添加了“使用存储”对于 filesystems.php 中的 ftp 并将我的控制器中的所有功能更改为 "Storage::" 类型。

问题是我的文件在上传到存储时损坏了。上传文件成功出现后(我已经尝试在本地和远程 ftp 存储上上传),但我无法打开它们,在放入我的文件时出现 “无法加载图像” 错误 /storage/app 文件夹和从远程存储打开 url 时的空白方块。当我使用 ftp_put(...) 之类的东西时,一切都运行良好。

我唯一注意到的是尝试打开 /storage/app 中的文件时给出的错误解释:

解释 JPEG 图像文件时出错(不是 JPEG 文件:以 0x2f 开头 0x76)

这意味着什么?我该如何处理这种情况?非常感谢任何可能的帮助!

UPD:在上传过程中某处的文件看起来不再是其本机格式的文件,然后被强行重命名,这会导致损坏。就像,我上传 .jpeg 文件,发生了一些事情,然后它在最后被保存为 .jpeg,不再是 .jpeg 了。还是不知道。

【问题讨论】:

  • 可能,问题是在使用 ftp_put() 的同时我还指定了模式 - FTP_BINARY,然后我删除了这个规范,因为没有在 Laravel 文档中被告知。就像,如果 ftp_put() 看起来像 ftp_put($conn_id, where_from, where_to, FTP_BINARY),在 Storage:: 中它就像 (where_from, where_to, visibility),关于模式什么也没说。所以,我不知道如何处理这些知识。
  • 您可以接受我给您的方法吗?还是你坚持十九世纪的风格?
  • @Arminius 我已经解决了这个问题,但如果你告诉我“正确”的方式,我将不胜感激

标签: php laravel ftp storage


【解决方案1】:

我明白了,问题是我把所有路径都留在了 () 中,就像它们在 ftp_put() 中一样,比如 (to, from),但是 Storage:: 需要内容,而不是路径,在 " from" 地方,所以 Storage::put(to, file_get_contents(from), 'public') 解决了我的问题。

【讨论】:

    【解决方案2】:

    这是为了提供信息,因为她已要求另一种方法。无需向上或向下拇指。

        public function store(Request $request){            
         $this->validate($request, array(
    // I have done the validations but skip to show it here
              // OBTAINING THE IMAGES 
                        $files = $request->images;               
                        // COUNTING HOW MANY WERE RECEIVED
                        $file_count = count($files);                
                        // INITIALIZING A COUNTER
                        $uploadcount = 0;       
    
                    foreach($files as $file) {
    
                        $filename = $file->getClientOriginalName();
    
                        $temporary = public_path(). '/uploads/temporary/' . $property->id;
                        if(!file_exists($temporary)) File::makeDirectory($temporary);
                        $temp = $file->move($temporary, $filename);  // This is where they temporary stay to be fetched for processing 
    
    
                        $thumbs = public_path(). '/uploads/thumbs/' . $property->id;
                        if(!file_exists($thumbs)) File::makeDirectory($thumbs);
                        Image::make($temp)->resize(240,160)->save($thumbs . '/' . $filename);
    
    
                        // We are setting up another directory where we want to save copies with other sizes
                        $gallery= public_path(). '/uploads/gallery/' . $property->id;
                        if(!file_exists($gallery)) File::makeDirectory($gallery);
                        Image::make($temp)->resize(400,300)->save($gallery . '/' . $filename);
    
                        $picture = new Picture;
                        $picture->property_id = $property->id;
                        $picture->name = $filename;
                        $picture->save();
                        $uploadcount ++;
    
                    }
    
    
                    if($uploadcount == $file_count){
                          Session::flash('success', 'Upload successfully'); 
                          return Redirect()->route('property.show', $property->id);
                        } 
                    else{ Session::flash('errors',  'screwed up'); 
                         return Redirect::to('upload')->withInput()->withErrors($validator);
                        }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2018-12-30
      • 2020-09-27
      • 2022-07-08
      • 2016-12-26
      • 1970-01-01
      相关资源
      最近更新 更多