Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phpmagician's avatar

Image upload with Data URI

I've got a form where users are able to update an avatar for their profile. . I'm wondering what the process/sequence of events is for uploading an image after a user submits the form (I don't want the upload to happen when the image is changed in the form). I'm able to capture the Data URI for the image source in the controller without a problem. But I'm wondering, how to convert that into an image that can be uploaded and the url stored in the database?

0 likes
4 replies
jekinney's avatar
Level 47

Post request as normal, where you process the request I use request->file() as in the docs. Laravel has helpers to move the file and you need to specify the path to move it to. I save that path and file name as the url to the image in the database too.

Files 101 video here Explains it along with the creating flyer series. Though the series uses Dropbox, the code is the same to process the image.

1 like
dharmendrajadon's avatar

You can put the data URI with this code: Here $photo is data URI

    private function savePhoto($photo)
    {
    $fileName = '';
    try {
        if(strlen($photo) > 128) {
            list($ext, $data)   = explode(';', $photo);
            list(, $data)       = explode(',', $data);
            $data = base64_decode($data);

            $fileName = mt_rand().time().'.jpg';
            file_put_contents('uploads/images/'.$fileName, $data);
        }
    }
    catch (\Exception $e) {
        $msg = $e;
    }
    return $fileName;
}
1 like
phpmagician's avatar

It was sort of a combination of both answers. @jekinney your answered helped with the manual upload I was doing and @dharmendrajadon your answered helped with uploading a file from drag and drop. Thanks!

1 like
rameezrami's avatar

Here is an improved answer of @dharmendrajadon :

private function savePhoto(Request $request)
{
$images = $request->get('images',[]);
        if(count($images)>0):
            foreach($images as $image):
                try {
                    if(strlen($image) > 128) {
                        list($mime, $data)   = explode(';', $image);
                        list(, $data)       = explode(',', $data);
                        $data = base64_decode($data);

                        $mime = explode(':',$mime)[1];
                        $ext = explode('/',$mime)[1];
                        $name = mt_rand().time();
                        $savePath = 'uploads/images/'.$name.'.'.$ext;

                        file_put_contents(public_path().'/'.$savePath, $data);

                        //saving file to File model
                        $file = new File();
                        $file->name = $name;
                        $file->path = $savePath;
                        $file->type = 'image';
                        $file->mime_type = $mime;
                        $file->extension = $ext;
                        $file->save();
                    }
                }
                catch (\Exception $e) {
                    //doing nothing here for not breaking the loop
                  // you can pass the error message to your view if you want.
                }
            endforeach;
        endif;
}

Please or to participate in this conversation.