1
votes

So I've been using ftp functions (manually setting $conn_id, making fpt_put($conn_id,...), conn_close etc) in my project,

now I've added "use Storage" in my controller, set host, username and password for ftp in filesystems.php and changed all the functions in my controller to "Storage::" type.

The problem is that my files get damaged while uploading on the storage. After upload files successfully appear (I've tried uploading on both local and remote ftp storage), but I can't open them, getting the "Could not load image" error on files put in my /storage/app folder and empty square when opening an url from the remote storage. While I was using ftp_put(...) and stuff, everything was working perfectly.

The only thing I've noticed is the error explanation given when trying to open a file placed in /storage/app:

Error interpreting JPEG image file (Not a JPEG file: starts with 0x2f 0x76)

What could this one mean and how could I handle this situation? Would highly appreciate any possible help!

UPD: looks like the file somewhere during the upload stops being a file of its native format, and then gets renamed back forcibly, which causes corruption. Like, I upload .jpeg file, something happens, then it gets saved with .jpeg at the end, not being a .jpeg anymore. Still no idea.

2
Possibly, the thing is that while using ftp_put() I also specified the mode - FTP_BINARY, and then I removed this specification, because nothing was told about it in Laravel docs. Like, if ftp_put() looks like ftp_put($conn_id, where_from, where_to, FTP_BINARY), in Storage:: it's like (where_from, where_to, visibility), and nothing is said about mode. So, I have no clue what to do with this knowledge. - Coffee
are you ok that I give you the way to do it right? or do you insist on doing it the XIX century style? - Arminius
@Arminius I have already solved this problem, but I would appreciate if you tell me about the "right" way anyways - Coffee

2 Answers

0
votes

Well I got it, the problem was that I left all the paths in () like they were with ftp_put(), like (to, from), but Storage:: requires contents, not path, in "from" place, so Storage::put(to, file_get_contents(from), 'public') solved my problem.

0
votes

This is for information purposes, since she has requested another way of doing it. No need to thumb it up or down.

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

        }