Copy image to storage folder

I’m trying copy an image to storage/app/uploads folder using the following code:

public function save(Request $request)
{
        $arrayName = array();
        echo "<pre>";
        print_r($request->all());
        echo "</pre>";

        $image = $request->file('image');
        $name = $imagem->getClientOriginalName();
        $ext = $imagem->getClientOriginalExtension();
        $newName = str_replace(' ','_', $imagem->getClientOriginalName());

        $destinationPath = 'uploads';
        //$path = $imagem->store($newName);
        $path = $imagem->storeAs($destinationPath, $newName);
        echo "<pre>";
        print_r($path);
        echo "</pre>";
}

The result from code above is:
Array
(
    [id] => 
    [description] => description
    [title] => title
    [title_small] => title_small
    [text] => text
    [image] => IlluminateHttpUploadedFile Object
        (
            [test:SymfonyComponentHttpFoundationFileUploadedFile:private] => 
            [originalName:SymfonyComponentHttpFoundationFileUploadedFile:private] => manaus1.png
            [mimeType:SymfonyComponentHttpFoundationFileUploadedFile:private] => application/octet-stream
            [error:SymfonyComponentHttpFoundationFileUploadedFile:private] => 1
            [hashName:protected] => 
            [pathName:SplFileInfo:private] => 
            [fileName:SplFileInfo:private] => 
        )

    [published] => S
)

But when I open what has been saved, it open a text document with the original extension.

I want save the right way.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m saving with this code

if(Input::hasFile('imagen')) {
   $time = Carbon::now()->format('Y-m-d');
   $image = $request->file('imagen');
   $extension = $image->getClientOriginalExtension();
   $name = $image->getClientOriginalName();
   $fileName = $time."-".$name;
   $image->move(storage_path(),$fileName);
}

Please try this and let me know how it works. 🙂

Method 2

I advise you to use this :

// SAME VALUES FROM THE FIRST ANSWER BUT WITH ANOTHER UPLOAD SYSTEM
if(Input::hasFile('imagen')) {
   $time = Carbon::now()->format('Y-m-d');
   $image = $request->file('imagen');
   $extension = $image->getClientOriginalExtension();
   $name = $image->getClientOriginalName();
   $fileName = $time."-".$name;
   // UPLOAD AND RUN YOUR SQL CODE IF YOU WANT ... 
   $path = Storage::putFile('your_path', $image);
   # LARAVEL WILL GENRATE A UNIQUE FILE NAME ;) 
   return dd($path);

   // OR RETURN JSON RESPONSE IF YOU USE AJAX ;) 
   return response()->json([
     'title' => $fileName,
     'path' => $path,
     'status' => 'success'
   ]);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x