ESP32 Arduino SPIFFS: Getting the size of a file

In this tutorial we will check how to obtain the size of the file from the ESP32 SPIFFS file system, using the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

 

Introduction

In this tutorial we will check how to obtain the size of the file from the ESP32 SPIFFS file system, using the Arduino core.

For an introduction on how to write to a file, please check here. For a tutorial on how to read from a file, please check this other post.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.


T
he code

As we did in the previous posts, we need to include the SPIFFS.h library. That way, we will have access to the SPIFFS extern variable, which we will use to interact with the file system.

#include "SPIFFS.h"

Moving on to the Arduino setup, we start by opening a serial connection, so we can later output the results of our program. We will also mount the SPIFFS file system, using the begin method of the already mentioned SPIFFS extern variable.

Serial.begin(115200);

if(!SPIFFS.begin(true)){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}

Now we will write some content to a file. Note that if you have already created and written to a file while following one of the previous tutorials, then you can skip this part if you want and use that file.

To open the file, we need to call the open method on the SPIFFS variable, passing as first input the name of the file to open and as second the opening mode. We will write to a file called “test.txt” and open the file in writing mode by passing as second parameter the constant FILE_WRITE.

This will return an object of class File and since this class overrides the C++ Boolean operator, we can check if the opening operation was successful simply by using an IF condition.

File file = SPIFFS.open("/test.txt", FILE_WRITE);

if(!file){
    Serial.println("There was an error opening the file for writing");
    return;
}

If the file was opened with success, we can proceed with writing some content to it. We will declare an arbitrary string and print its size before writing it to the file. Since I’m using a const char * rather than the Arduino String class, it is possible to obtain its size in bytes using the strlen function.

const char * content = "Testing content!";

Serial.print("Content length: ");
Serial.println(strlen(content));

We then proceed with writing the content to the file and doing an error check to confirm that the content was written.

We finish the writing procedure by calling the close method on our file object.

if(file.print(content)){
    Serial.println("File was written");;
} else {
    Serial.println("File write failed");
}

file.close();

Now that we have the file with some content, we will open it in read mode, since we are no longer going to modify it but rather simply read its size.

We do that again by using the open method of the SPIFFS variable but, this time, we don’t need to explicitly pass the opening mode since that is an optional argument that defaults to reading mode (the one we want). This will again return an object of class File.

After that, we check again if the file was open successfully before trying to use it.

File file2 = SPIFFS.open("/test.txt");

if(!file2){
    Serial.println("Failed to open file for reading");
    return;
}

On success, we can obtain the size of the file simply by calling the size method on our File object. This method takes no arguments and returns the size of the file, in bytes.

Serial.print("File size: ");
Serial.println(file2.size());

To finalize the procedure, we close the file.

file2.close();

The final source code can be seen below.

#include "SPIFFS.h"

void setup() {

   Serial.begin(115200);

   if(!SPIFFS.begin(true)){
        Serial.println("An Error has occurred while mounting SPIFFS");
        return;
   }

    File file = SPIFFS.open("/test.txt", FILE_WRITE);

    if(!file){
        Serial.println("There was an error opening the file for writing");
        return;
    }

    const char * content = "Testing content!";

    Serial.print("Content length: ");
    Serial.println(strlen(content));

    if(file.print(content)){
        Serial.println("File was written");;
    } else {
        Serial.println("File write failed");
    }

    file.close();

    File file2 = SPIFFS.open("/test.txt");

    if(!file2){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("File size: ");

    Serial.println(file2.size());

    file2.close();
}

void loop() {}


Testing the code

To test the previous code, simply compile it and upload it to your device using the Arduino IDE. Upon completion, open the Arduino IDE serial monitor. You should get an output similar to figure 1, which shows both the size of the content written and the size of the file, which match.

ESP32 Arduino SPIFFS file system get size of file.png

Figure 1 – Getting the size of the file in the ESP32 SPIFFS file system.


Related Posts

12 thoughts on “ESP32 Arduino SPIFFS: Getting the size of a file”

    1. Hi!

      What do you mean by downloading them? Like having a webserver on the ESP32 and being able to remotely get the files, or having some kind of program to download them offline?

      For the first case, I think the asynchronous HTTP webserver allows to do it, although I haven’t yet tested:
      https://github.com/me-no-dev/ESPAsyncWebServer#respond-with-content-coming-from-a-file

      To retrieve them “offline”, I’m not aware of any tool, just to upload the files (I haven’t yet tested it also):
      https://github.com/me-no-dev/arduino-esp32fs-plugin

      Hope this helps 🙂

      Best regards,
      Nuno Santos

      1. Hello,
        thanks for your reply.

        I would like to insert a downloadable file as a link in the web page presented by the ESP32 web server:
        Click to download

        For uploading, I have seen this video, but I have not tried it yet:

        1. Hi,

          You’re welcome 🙂

          Unfortunately I haven’t yet tested it, but I think in that case the best solution to explore is indeed the async HTTP web server libraries and the integration with SPIFFS.

          Their example might be useful:
          https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino

          I’m planning to test the integration of the HTTP webserver with the file system since it seems very promising and it is very useful, but unfortunately I’m having some really busy weeks and I’m not sure when I’m going to be able to do it.

          When I manage to do it, I will make sure to make a post 🙂 In the meanwhile, if you succeed on testing it, please let us know, since it will definitely be very useful to others.

          Best regards,
          Nuno Santos

    1. Hi!
      What do you mean by downloading them? Like having a webserver on the ESP32 and being able to remotely get the files, or having some kind of program to download them offline?
      For the first case, I think the asynchronous HTTP webserver allows to do it, although I haven’t yet tested:
      https://github.com/me-no-dev/ESPAsyncWebServer#respond-with-content-coming-from-a-file
      To retrieve them “offline”, I’m not aware of any tool, just to upload the files (I haven’t yet tested it also):
      https://github.com/me-no-dev/arduino-esp32fs-plugin
      Hope this helps 🙂
      Best regards,
      Nuno Santos

      1. Hello,
        thanks for your reply.
        I would like to insert a downloadable file as a link in the web page presented by the ESP32 web server:
        Click to download
        For uploading, I have seen this video, but I have not tried it yet:

        1. Hi,
          You’re welcome 🙂
          Unfortunately I haven’t yet tested it, but I think in that case the best solution to explore is indeed the async HTTP web server libraries and the integration with SPIFFS.
          Their example might be useful:
          https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/examples/ESP_AsyncFSBrowser/ESP_AsyncFSBrowser.ino
          I’m planning to test the integration of the HTTP webserver with the file system since it seems very promising and it is very useful, but unfortunately I’m having some really busy weeks and I’m not sure when I’m going to be able to do it.
          When I manage to do it, I will make sure to make a post 🙂 In the meanwhile, if you succeed on testing it, please let us know, since it will definitely be very useful to others.
          Best regards,
          Nuno Santos

  1. Pingback: ESP32 SPIFFS: Append content to file | techtutorialsx

  2. Pingback: ESP32 SPIFFS: Append content to file | techtutorialsx

  3. Pingback: ESP32 Arduino SPIFFS: Append content to file | techtutorialsx

  4. Pingback: ESP32 Arduino SPIFFS: Append content to file | techtutorialsx

  5. Pingback: ESP32 Arduino SPIFFS: File upload IDE plugin | techtutorialsx

  6. Pingback: ESP32 Arduino SPIFFS: File upload IDE plugin | techtutorialsx

  7. Pingback: ESP32 web server: Serving HTML from file system | techtutorialsx

  8. Pingback: ESP32 web server: Serving HTML from file system | techtutorialsx

Leave a Reply