How can I do to insert an image into a database?

Hi everyone, I am new to using MODX

In fact, I am creating a program to insert information into a database. I’m stuck at a point that I need to create in the table an option to add image, i.d, I need to add a new field in the xml schema where the image is saved and create the query that allows me to load the image directly to the database

<?xml version="1.0" encoding="UTF-8"?>

Thanks, I hope your answer.

<?xml version="1.0" encoding="UTF-8"?>
<model package="incidents" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    <object class="IncidentsDescription" table="Reports" extends="xPDOSimpleObject">
        <field key="Client" dbtype="int" precision="255" phptype="integer" null="false" default=""/> 

whats the reason to store the image into the db?
Usually, you would upload it as file and just store the filename into the db.

Thanks Bruno17 for your answer.

The reason is that I need to save all the information in the database because I export later in a data table.

If you tell me that there is another method, could you please tell me how to upload it as a file and store it in the database or if there is a tutorial?

I have not seen a MODx example, but there can be a fair amount of complexity to accomplish what you want.

Idea for MODX:
The “content” field on a resource is a MEDIUMTEXT field which should store up to 16MB of data or ~16Million characters. You could create a content type for images and simply store the base 64 Encoded data in the Content field.

  • You would need an “Image” template with a snippet to decode the content from Base64.
  • You would need to carefully restrict the image sizes so that they don’t exceed the field length limits
  • Make sure that the snippet is using the uncached flag

Other example structure:
One complex/robust solution I have seen in a different commercial product does the following:

  1. Transfer the image to the server
  2. Base64 encode the image
  3. Break the encoded string into blocks of a certain size (not sure the exact size they use)
  4. Store an attachment record that has the metadata around the file/image including things like MIME Type, file size, etc.
  5. Store the encoded blocks as child records related to the parent “attachment”
  6. The uploaded file is discarded after successful save

Then to render the image back to the client you would need a snippet or endpoint. In this particular solution, you go to a /<id_of_parent_attachment>.

That processor has to reverse and put together all the data.

  1. Query for all the child data blocks
  2. Concatenate back to a single Base64 string
  3. Decode it back to the source type
  4. Apply filename and needed details from the metadata attachment record
  5. Trigger the download back to the client

As you can see there is a fair amount of overhead. You could definitely do this in MODX, but you may need a custom table.

I believe one of the reasons they break the attachment into blocks is file size issues and database field types. Base64 encoding the file also increases the total data storage requirements since the string encoding data is larger than the file.

There are benefits for the trade-offs, but you’d really have to have a strong reason I think. One of the biggest benefits can be security of access to the file data potentially on a shared application or similar. You can apply an application level security model that enforces more granularity than with the file structure (I think, in general).

Anyway, hope that helps.


Actually, maybe I’m misunderstanding. Do you just mean, upload the file to a directory and store the file path in a database field? Then when you output the list you have a link to download the image?

what is the use case of that exported data table? What format should the exported data table have?

Yes, I wanna that. it’s more easier than other method, like said Bruno 17.

The best way to handle this would be a semi DAM type of system where you’d save information about the image in a table and then reference it.

A good way to do this is to create a plugin that listens for the OnFileManagerUpload ( event. Then you’d check the file’s MIME type to see if its an image, then save information about the image to your table along with the file path.

This way images stay on the server and are referenced in your DB.

Thanks to all for help me in this problem.

Reading and following each tip, I did these step.

first, I call the name of the form.

` $nameFile = $_FILES['miArchivo']['name'];`

second, I create a file using mkdir into a function

   if (!file_exists($directorio)) {
            mkdir($directorio, 0777, true);
            if (file_exists($directorio)) {
                if (move_uploaded_file($guardado, 'archivos/' . $nameFile)) {
                    //echo "Archivo guardado con exito";
                } else {
                    // echo "Archivo no se pudo guardar";
        } else {
            if (move_uploaded_file($guardado, $directorio . $nameFile)) {
                //echo "Archivo guardado con exito";
            } else {
                //echo "Archivo no se pudo guardar";

            return $ruta;
    } else {
        // File type not allowed
        echo 'Type of file not allowed.';
        return false;

third, I saved the return inside a var
$newReport->url_image = uploadImage();

And the last step is call the var from database and using the tag img show the imagen inside of the data table.

Did this work out for you? It’s not quite clear if you achieved success of if you still need some assistance.

some links, which could help