首页 文章

Laravel存储文件在上传时会损坏

提问于
浏览
1

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

现在我已经在 my controllerset hostusernamepassword 为filesystems.php中的ftp添加了"use Storage",并将我控制器中的所有功能更改为 "Storage::" 类型 .

问题是我的文件在存储上传时会被损坏 . 上传文件成功出现后(我打开它们,在我的 /storage/app 文件夹中放置文件时出现 "Could not load image" 错误,从远程存储打开网址时出现空方格 . 当我使用_1763172时,一切正常 .

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

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

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

UPD:在上传过程中看起来像某个地方的文件是其原生格式的文件,然后被强制重命名,这会导致损坏 . 就像,我上传.jpeg文件,发生了什么事情,然后它最后以.jpeg保存,而不再是.jpeg . 仍然不知道 .

2 回答

  • 0

    这是出于信息目的,因为她已经要求采用另一种方式 . 无需向上或向下拇指 .

    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);
                        }
    
            }
    
  • 0

    好吧,我明白了,问题是我把()中的所有路径都放在ftp_put()中,比如(to,from),但 Storage:: requires contents, not path, in "from" place ,所以 Storage::put(to, file_get_contents(from) ,'public')解决了我的问题 .

相关问题