Hi,
I am trying to save the moved file path to another function.
The file gets uploaded then moved, I have it in a form which is using store.
I want to be able to save the final path to my item column in store.
In short I would like to get the last moved path to my store function and save it in the item column when the form is submitted I keep getting this error with what I currently have:
Unresolvable dependency resolving [Parameter #0 [ string $path ]] in class Symfony\Component\HttpFoundation\File\UploadedFile
or variable is not defined, though I have no idea how it can't be defined if it is defined in saveFile function.
The file upload and saving
/**
* Saves the file
*
* @param UploadedFile $file
*
* @return \Illuminate\Http\JsonResponse
*/
protected function saveFile(UploadedFile $file)
{
$fileName = $this->createFilename($file);
// Group files by the date (week
$dateFolder = date("Y-m-W");
// Build the file path
$filePath = "upload/{$dateFolder}/";
$finalPath = storage_path("app/".$filePath);
// move the file name
$file->move($finalPath, $fileName);
return response()->json([
'path' => $filePath,
'name' => $fileName
]);
}
/**
* Create unique filename for uploaded file
* @param UploadedFile $file
* @return string
*/
protected function createFilename(UploadedFile $file)
{
$extension = $file->getClientOriginalExtension();
$filename = str_replace(".".$extension, "", $file->getClientOriginalName()); // Filename without extension
// Add timestamp hash to name of the file
$filename .= "_" . md5(time()) . "." . $extension;
return $filename;
}
My store function that submits the form with the one input field
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$clientupload = new ClientUpload;
$clientupload->form_id = $request->form_id;
$clientupload->item = $this->saveFile($request, $finalPath);
$form->save();
return redirect(route('client-upload.index'))->with('success', 'Upload added successfully.');
}
My create form if need be
<form action="{{route('client-upload.store')}}" method="POST">
@csrf
<div class="row">
<div class="col-md-3">
<label for="form_id">Job No</label>
<select class="form-control" name="form_id" id="form_id">
@foreach($forms as $form)
<option value="{{$form->job_description}}">{{$form->job_description}}</option>
@endforeach
</select>
</div>
</div>
<div class="row">
<div class="col-md-12 pt-3">
<div class="text-center" >
<div id="resumable-error" style="display: none">
Resumable not supported
</div>
<div id="resumable-drop" style="display: none">
<p>
<button id="resumable-browse" data-url="{{ route('cupload') }}" >Upload</button> or drop here
</p>
</div>
<ul id="file-upload-list" class="list-unstyled" style="display: none">
</ul>
<br/>
</div>
</div>
</div>
<div class="text-center pt-2">
<button class="btn btn-primary btn-block" type="submit">Submit</button>
</div>
</form>