fabianobn's avatar

Help in this code to upload and delete image on public folder

Hello everyone! How are you?

I'm having a hard time uploading a photo and deleting it.

Sending is okay, but when I delete the image and send a new one the old photo that is returning and not the new one.

What am I doing:

I use the webcam to take the photo and in base64 convert with the base64_decode of laravel.

But deleting I think that just deleting already solves, but it is not!

Functions:

public function saveImagem($img,$id,$path)
{
    //$img = base64_decode($img);
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $imgName = $id.'.png';
    $path = public_path().'/uploads/'.$path.'/'.$imgName;
    \File::put($path, base64_decode($img));
    return $imgName;
}

public function destroyImagem($photo, $path)
{
    $imgName = $photo.'.png';
    $fileDir = public_path().'/uploads/'.$path.'/';
    if(\File::exists($fileDir . $imgName)){
        \Storage::delete($fileDir . $imgName);
        return true;
    }
    return false;
}

Can someone help me?

Thanks in advance!

0 likes
3 replies
Snapey's avatar

you're not kidding,

have a look at Intervention package. You can pass it the base64 stream and the save a resized image in a choice of formats to any local storage

fabianobn's avatar

I believe I do not need to use another library because on the local server on my computer I have tested and it worked. but on the server where it is hosted it is not working. It even deletes the image more when sending again is coming the previous print. As if the previous image was cached.

Please or to participate in this conversation.