Reputation: 127
I create one form to upload profile pictures before with following code,
$image = $request->file('avatar');
$pass = Hash::make($request->password);
if($image!=null){
$avatar_name = $request->get('NIC').'.'.$image->getClientOriginalExtension();
$folder = '/uploads/avatars/';
// $filePath = $folder . $avatar_name;
$this->uploadOne($image, $folder, 'public', $avatar_name);
DB::table('users')->insert([
[
'avatar_id' => $avatar_name,
.
.
.
.
public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
{
$name = !is_null($filename) ? $filename : Str::random(25);
$path =storage_path('uploads/avatars/'.$filename.'');
if( Storage::exists($path)){
Storage::disk('public')->delete('uploads/avatars/'.$filename.'');
}
$file = $uploadedFile->storeAs($folder, $name, $disk);
return $file;
}
<form method="post" action="{{ route('edit_user.store') }}" enctype="multipart/form-data" autocomplete="off">
@csrf
<div class="pl-lg-4">
<input type="text" name="user_id_" id="input-user_id" hidden>
<fieldset name="Personal_information">
<legend class="heading-small text-muted mb-4" >Personal information</legend>
<div class="form-group{{ $errors->has('avatar') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-avatar">{{ __('Avatar') }}</label>
<input type="file" name="avatar" id="input-avatar" class="form-control form-control-alternative{{ $errors->has('avatar') ? ' is-invalid' : '' }}" >
@if ($errors->has('avatar'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('avatar') }}</strong>
</span>
@endif
</div>
Now I make another page to upload some documents with following code,
$id = auth()->id();
$timestamp_=now()->toDateTimeString();
$doc = $request->file('leave_document');
if($doc!=null){
$leave_doc_name = $id.'_'.$timestamp_.'.'.$doc->getClientOriginalExtension();
$folder = '/uploads/leave_documents/';
$filePath = $folder . $leave_doc_name;
$this->uploadOne($doc, $folder, 'public', $leave_doc_name);
}else{
$leave_doc_name =null;
}
if($request->leave_id_=="add"){
DB::table('leave_requests')->insert([
[
'document_id'=>$leave_doc_name,
.
.
.
.
.
public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
{
$name = !is_null($filename) ? $filename : Str::random(25);
$path =storage_path('uploads/leave_documents/'.$filename.'');
if( Storage::exists($path)){
Storage::disk('public')->delete('uploads/leave_documents/'.$filename.'');
}
$file = $uploadedFile->storeAs($folder, $name, $disk);
return $file;
}
<form id="addEventForm" method="post" enctype="multipart/form-data" action="{{ route('leave.store') }}" autocomplete="off">
@csrf
<div class="pl-lg-4">
<input type="text" name="leave_id_" id="input-leave_id" hidden>
<div class="form-group{{ $errors->has('leave_document') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-leave_document">{{ __('Document') }}</label>
<input type="file" name="leave_document" id="input-leave_document" class="form-control form-control-alternative{{ $errors->has('leave_document') ? ' is-invalid' : '' }}" >
</div>
As you can see both codes are the same. The only major difference is the storage folders. (first one user "\storage\app\public\uploads\avatars" folder and second one uses "storage\app\public\uploads\leave_documents" folder.
but for the second one, I get an error saying
fopen(C:\xampp\htdocs\ACCHE_HR\storage\app/public\uploads/leave_documents/2_2020-03-15 19:31:14.jpg): failed to open stream: No such file or directory
So it seems symlink only work for avatar folder I try to remove symlink using "rm public/storage" command and it also gives me the following error,
rm : C:\xampp\htdocs\ACCHE_HR\public\storage is an NTFS junction point. Use the Force parameter to delete or modify this object. At line:1 char:1 + rm public/storage + ~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\xampp\htdocs\ACCHE_HR\public\storage:DirectoryInfo) [Remove-Item], IOException + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
and I search everywhere, also try to find a way to create custom symlinks and failed. How do I resolve this problem, Also I'm new to Laravel?
Please Help!
Upvotes: 0
Views: 410
Reputation: 127
When I change my file name to something like this
$leave_doc_name = 'abc.'.$doc->getClientOriginalExtension();
From
$leave_doc_name = $id.'_'.$timestamp_.'.'.$doc->getClientOriginalExtension();
It works.
It seems the timestamp function doesn't work with the file upload.
Upvotes: 1