ESP32/Storage/36. Uploading files to LittleFS
Your chip
Your language
Storage · 36 of 81

Uploading files to LittleFS

Your sketch and your files are two separate uploads to two separate partitions. Knowing that explains why re-flashing does not delete your web page, and why re-uploading the folder does.

/esp32/uploading-files-to-littlefs · arduino · S3

Two uploads, two partitions

data/ → LittleFS partition
0 B of 1.4 MB
What is in data/
Folder
312 kB
Written
0 B
Dropped
none
One folder, one button. Everything under data/ goes in, keeping its name and its path. Nothing else does: a file one level up is invisible to the tool.

The sketch goes into the app partition. Everything under data/ in your sketch folder goes into the filesystem partition. They are different regions of the same flash chip with different sizes decided by the partition table, and one upload does not touch the other.

That single fact answers most of the questions people ask about this:

  • Re-flashing the sketch does not erase your files.
  • Re-uploading the folder replaces the whole filesystem, including anything the sketch wrote at runtime.
  • "Erase all flash before sketch upload" erases both.

The steps

  1. Make a folder called data next to your .ino file, not inside any other folder.
  2. Put the files in it, with the paths you want them to have. data/www/app.js becomes /www/app.js on the board.
  3. Run the upload command. In Arduino IDE 2.x it is in the command palette (Ctrl+Shift+PUpload LittleFS to Pico/ESP8266/ESP32), not the Tools menu.
  4. Close the Serial Monitor first. It holds the port, and the upload needs it.

Why LittleFS and not SPIFFS

SPIFFS is deprecated and has no directories, no power-loss safety worth the name, and gets slower as it fills. LittleFS has all three and is built into the core, so there is no library to install. Every SPIFFS example on the internet works if you change the header and the object name, and there is no reason left to start a new project on it.

Watch the size

A microcontroller's filesystem is measured in hundreds of kilobytes. The default 4 MB layout leaves about 1.4 MB. One web font and one logo can spend most of it, and nothing warns you — so gzip what you serve, and check usedBytes() in the same sketch that mounts it.

On your S3
ChipXtensa LX7 · 2 × 240 MHz
Board settingESP32S3 Dev Module
Default I2CSDA 8 · SCL 9
Watch out forThe port vanishes after upload

The code

After the folder upload, this proves the files arrived — it mounts the filesystem and lists what is in it with sizes.

readfiles.ino
#include <LittleFS.h>

void setup() {
  Serial.begin(115200);
  delay(200);
  if (!LittleFS.begin(false)) {          // true = format if the mount fails
    Serial.println("mount failed — was the data folder ever uploaded?");
    return;
  }
  Serial.printf("%u of %u bytes used\n", LittleFS.usedBytes(), LittleFS.totalBytes());

  File dir = LittleFS.open("/");
  for (File f = dir.openNextFile(); f; f = dir.openNextFile())
    Serial.printf("  %-20s %6u\n", f.name(), f.size());
}

void loop() {}

Passing true to begin() formats the partition if the mount fails. Convenient on a first run, and a silent way to erase everything on a board in the field.

When it does not work

The upload tool is not in my Tools menu

Arduino IDE 2.x uses a different mechanism from 1.x. Install the arduino-littlefs-upload extension, put the .vsix in the IDE's plugins folder, restart, and the command appears in the Ctrl+Shift+P palette rather than in Tools.

Mount fails after the sketch uploads fine

The partition scheme has no filesystem in it. Several schemes — the OTA ones especially — spend the whole chip on two app slots. Tools → Partition Scheme, pick one whose name mentions SPIFFS or FATFS, and upload the folder again.

Some files uploaded and some did not

The partition filled up. Nothing errors; the tool writes until there is no room and stops. Check usedBytes against totalBytes, and remember that a web font and a PNG will each be larger than all your code.

The files vanished after I flashed a new sketch

Only if the partition table changed. Changing the partition scheme moves the filesystem's start address, so the old contents are still in flash and no longer where anything looks for them. Re-upload the folder after any scheme change.

Where this goes next

A filesystem is the wrong place for a Wi-Fi password and a calibration constant. For those there is a smaller store with wear levelling built in.

Preferences and NVS

Edit this page — content/esp32/uploading-files-to-littlefs.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse ESP32 on the forum