Core\Request\Part::filename
Returns the file name that the client sent with an uploaded file, such as invoice.pdf. This is the name the file had on the client's computer.
The result is a tainted string, which means it came from outside your program. The client can send any name: an empty one, a very long one, or one like ../../config.toml that points to another folder. You cannot use it as a path directly. Core\IO::within checks that the name stays inside a folder you choose. A safer way is to save the file under a name your program makes, and to use the client's name only for display.
Good to know: when a person submits a form without choosing a file, the browser still sends the field. Its file name is then empty.
The examples show the name of each file, how to skip a file field that was left empty, and how to save each file under a new name.
Return value
Returns tainted string. The name the client sent, tainted. It cannot be used as a file path until Core\IO::within checks it. This stops a client from choosing where its upload is saved.
Examples
The name of each file
<?nvs// Shows the file name that the client sent with each uploaded file.
foreach (Core\Request::files() as Core\Request\Part $part) { // `filename` returns the name the file had on the client's computer. // This prints "holiday.jpg" and then "map of the route.png". echo $part->filename(), "\n";}holiday.jpgmap of the route.pngA file field left empty
<?nvs// Shows how to skip a file field that the person left empty.
// A form with two file fields sends both, even when the person chose only one file.// The field without a file has an empty file name.uint $received = 0;foreach (Core\Request::files() as Core\Request\Part $part) { if ($part->filename() == '') { echo 'no file was chosen for ', $part->name(), "\n"; continue; } $received = $received + 1;}
// This prints "1 file received".echo $received, " file received\n";no file was chosen for photo1 file receivedSaving under a new name
<?nvs// Shows how a website saves each upload under a name it makes, and keeps the client's name for display.
string $folder = Core\IO::createTemporaryDirectory();uint $number = 0;foreach (Core\Request::files() as Core\Request\Part $part) { $number = $number + 1; // The program chooses the file name on disk, so the client cannot choose the folder. string $stored = 'upload-' . $number . '.bin'; $part->saveTo(Core\Path::join($folder, $stored)); // The client's name is only printed. The second name prints as "../../index.nvs". echo $part->filename(), ' is saved as ', $stored, "\n";}contract.pdf is saved as upload-1.bin../../index.nvs is saved as upload-2.bin