What do you get if you dump the path? Can you find the file in that location?
$target = public_path('storage/documents/download');
dd($path.$fileName);
\File::copy($path.$fileName, $target.$fileName); // errors here
I am using File::copy to copy the uploaded file from the main folder into my target folder. But it keeps saying there is no such file or directory.
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required',
'product_id' => 'required',
'price' => 'required',
'file' => 'required',
'file.*' => 'mimes:jpeg,jpg,png,svg,gif,csv,xlsx,pdf,docx',
'publish' => 'required',
]);
$fileData = [];
if($request->hasFile('file'))
{
foreach($request->file('file') as $file)
{
$path = public_path('storage/documents/'.$id);
$fileName = time().'_'.$file->getClientOriginalName();
$file->move($path, $fileName);
$target = public_path('storage/documents/download');
\File::copy($path.'/'.$fileName, $target.'/'.$fileName) // errors here
$fileData[] = $fileName;
}
$data['file'] = json_encode($fileData);
}
$document = Document::create($data);
return redirect()->route('product.index')->with('success','Document added successfully');
}
How can I solve this problem or is there any other way to upload same file on 2 folders?
Thanks, @siangboon . I found the solution, the target directory needs to be created first to store the copied files from the main directory.
if (!File::exists(public_path('storage/documents/download'))) {
File::makeDirectory(public_path('storage/documents/download'), 0775, true);
}
Please or to participate in this conversation.