Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel move uploaded file

It is working good for full path like this

$file=$request->file('file');
$file->move('C:\xampp\htdocs\modo\images',$file->getClientOriginalName());

But i cant understand why it doesnt for root folder path :

$file->move('\modo\images',$file->getClientOriginalName());
like image 580
Kamran G. Avatar asked Jun 08 '16 05:06

Kamran G.


People also ask

How do I move an image in Laravel?

Below the Laravel approach for moving after upload. From the documentation: $request->file('photo')->move($destinationPath); $request->file('photo')->move($destinationPath, $fileName); photo is the name of your file upload input element.

Does move_uploaded_file overwrite?

The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How do I move a file into permanent directory?

PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files to a permanent directory, if you want to keep them permanently. PHP offers the move_uploaded_file() to help you moving uploaded files. The example script, processing_uploaded_files.


2 Answers

You need to use base_path() method. This method returns the fully qualified path to the project root:

So in your case try the below code:

$file = $request->file('file');
$file->move(base_path('\modo\images'), $file->getClientOriginalName());

and if you want to return the public directory then use:

$path = public_path();

For more info read Laravel helper functions

like image 110
Iftikhar uddin Avatar answered Oct 20 '22 00:10

Iftikhar uddin


You need to do it this way:

$file->move(base_path('\modo\images'),$file->getClientOriginalName());
like image 40
GONG Avatar answered Oct 20 '22 01:10

GONG