YuMp's avatar
Level 2

Livewire upload direct to public folder question

Hello folks. I'm facing a problem with livewire. I need to take the files to the public/media/uploads folder, but it's only saving in livewire-tmp. Does anyone have a solution for me to get around this issue? Thanks in advance.

code --->

class CreatePost extends Component { use WithFileUploads;

public $title;
public $category = 1;
public $description;
public $image;


protected $rules = [
    'image' => 'nullable|image|mimes:jpeg,jpg,png|max:18048',
    'title' => 'required|min:4',
    'category' => 'required|integer|exists:categories,id',
    'description' => 'required|min:4',
];



public function createPost(Request $request)
{
    if(auth()->check()){

 // IMAGE JPG,PNG,SVG
 $randomNumberimg = random_int(1000000000000, 9999999999999);
 if ($image = $request->file('image')) {
     $destinationPath = 'media/uploads';
     $pImage = date('YmdHis') . '_n' . $randomNumberimg. "." . $image->getClientOriginalExtension();
     $image->move($destinationPath, $pImage);
     $imgpost['image'] = "$pImage";
 }




    $this->validate([

        'image' => 'nullable|image|mimes:jpeg,jpg,png|max:18048',
        'title' => 'required|min:4',
        'category' => 'required|integer|exists:categories,id',
        'description' => 'required|min:4',
        ]);
    Post::create([
        'user_id' => auth()->id(),
        'category_id' => $this->category,
        'status_id' => 1, 
        'title' => $this->title,
        'description' => $this->description,
        'image' => $imgpost['image'] ?? '',

        

    ]);
    session()->flash('success_message', 'Post Adicionado com sucesso');
    $this->reset();

    return redirect()->route('post.index');
    }

    abort(Response::HTTP_FORBIDDEN);
}
0 likes
5 replies
vincent15000's avatar

Have you tried this ?

$destinationPath = public_path('media/uploads');

But it's strange to upload in the public path. It's better to upload in the storage path and then give public access to the storage path via a symbolic link from the public path.

YuMp's avatar
Level 2

@vincent15000 Hi, thanks for your reply. I tried but to no avail it is still being saved to livewire-tmp.

1 like
YuMp's avatar
Level 2

@deansatch Hello, I basically applied it by changing the path in the filesystem. My only problem currently is to send for example a video using the same idea as the image I get the error if I use it this way. would have any idea how I can solve this issue because in my form there are these two entries for posting. 'image' => $this->image->store('images'), 'video' => $this->video->store('videos'),

Call to a member function store() on null

thanks in advance :)

Please or to participate in this conversation.