hendraspt hendraspt - 3 months ago 9
PHP Question

How to save the full path of file upload in php?

I am using Laravel and I have a form that uploading a file. And I want to save the full path of that file in my database. Do you know how?

if($request->hasFile('alternate_add_file_path')) {
$file = $request->file('alternate_add_file_path');
$destination = 'files/';
$extension = $file->getClientOriginalExtension();
$file_name = $getCreateDate . '_' . str_replace('/','_',$M_USER_NAME) . '.' . $extension;
$file->move($destination, $file_name );
}


This below is for the save into database

$M_FILE_PATH = $file_name;


I do not have an error though, just only the filename which saved into my database, not the full path.

Answer

To convert a relative path to an absolute one (server side) you can use realpath. Eg.

$fullpath = realpath($file_name);