Uploading an image to databa


I really need some help, i am new to Slim. how do i upload an image to data base and also fetch the uploaded image in slim framework.


Hi Nze. This is a very open question.

  • How far have you gotten?
  • How are you uploading the image? (HTML form)
  • What kind of database are you using?


Thanks for responding. it is HTML form.
MySQL database.

my connections are ok and i’m able to upload other thing apart from the


So you are able to upload other file types into your database, but just not images? What field type are you using in the database to store the images? Are you able to upload them to the filesystem to isolate if it is a database issue?


i think the database is good. I think the problem should be the query to update the database


Use database for store images is not a good idea.

You should save on your database table a logic that helps you to retrieve the image from the filesystem (or the service that store it).
For a very basic approach you save on a column the path of the stored image.


We have recently done this.
We do save the name of the image into a column in a table … but handle the saving of images through SLIM Post like this … in a case statement within the post method we look out for the path “AssetImage”

       case 'AssetImage':
        $images_path = __DIR__."/../client_images/";
            //make sure you have a folder called uploads where this php file is
            if (!is_dir($images_path.$clientId)) {
            mkdir($images_path.$clientId, 0777, true);
        $this->logger->debug('Path = ' .$images_path.$clientId);
        * borrowed this logic from here .... 
        * http://computingforgeeks.com/upload-images-or-files-with-slim-php-and-angularjs/
        $files = $request->getUploadedFiles();
        $keys = array_keys($files);
        foreach($keys as $key)
            $file = $files[$key];
                $message['msg']="Encountered an error: while uploading.no FILE UPLOADED";
                $this->logger->debug('Error message = ' .$error['msg']);
                $httpStatus = 400;

                   $name = $file->getClientFilename();

                   $this->logger->debug('Uploaded File Name = ' .$name);

                   //make sure you have a folder called uploads where this php file is
                   if (!is_dir($images_path.$clientId)) {
                            mkdir($images_path.$clientId, 0777, true);
                        $file-> moveTo($images_path.$clientId."/".$name);
                        $message['msg']="Image updated successfully";
                        $httpStatus = 200;
                   catch(Exception $e)
                       $this->logger->critical("Error writing image: ".$e->getMessage());

                        $message['msg']="Encountered an error: while uploading. NO FILE UPLOADED";
                        $httpStatus = 500;
        return $response->withJson($message, $httpStatus);


Oh … and Fetching the image …

We store the image name in a column in the table called (in our case) Assets
then to display the image on a form for the asset that the user has selected…

the images area stored into folders with names matching the user’s unique ID (in out case that’s a GUID)

<img src=url-of-site+'/client_images/'+current.asset.ClientID+'/'+current.asset.ImageName />