POV
POV

Reputation: 12015

How to handle file in Laravel?

The client sends file to server. It look like this:

array:1 [▼
  "file" => array:5 [▼
    "name" => "MMM1(one row).TXT"
    "type" => "application/octet-stream"
    "tmp_name" => "/tmp/phpaKnJzE"
    "error" => 0
    "size" => 1365
  ]
] 

If to make the following operation:

dd($_FILES);

I tried to handle thi file like:

foreach ($_FILES["file"] as $file) {
   $file->store('tests');
   $file->getClientOriginalName();
}

But it does not work for me.

It involke an error:

Call to a member function getClientOriginalName() on array

$this->file->getClientOriginalName()

Upvotes: 0

Views: 1511

Answers (2)

asmmahmud
asmmahmud

Reputation: 5044

If You're uploading an image then you can also use intervention/image package. Using this, you'll also be able to do basic image manipulation.

In you controller post action do the following:

 use Intervention\Image\Facades\Image;

if($request->hasFile('image')){
    $image    =  \Image::make( $request->file( 'image' ) );
    $image->save( storage_path('images/'. $request->file('image')->getClientOriginalName()));
}

Upvotes: 0

ankit patel
ankit patel

Reputation: 1918

You need to set below code in your post action

$photo = $request->file('img');
$path = storage_path('app/public/avatars/');
$photo->move($path, $request->file('img')->getClientOriginalName());

Upvotes: 2

Related Questions