store()

Moves the uploaded file to a folder.

Table of Contents
  1. Description
  2. Example

Description

store(string $path, array $blob, ?string $as = null): ?false|int|string;

This function makes it easy for you to put the blob data returned by $_POST variable. Unlike the usual PHP upload mechanism, this application will make a copy of the data from $_FILES variable to $_POST variable and then will change its array structure to make it easier for data consumers to iterate over. This function will return the file path of the blob that was successfully moved or false if the file path already exists, or in the form of an error code, or null if the move process failed.

Let’s say you have an HTML structure of a form like this, along with the PHP code to initialize the file upload process:

<?php

if ('POST' === $_SERVER['REQUEST_METHOD']) {
    test($_FILES, $_POST);
    exit;
}

?>

<form enctype="multipart/form-data" method="post">
  <input name="blob" type="file">
  <input min="1" name="blob[height]" step="1" type="number" value="768">
  <input min="1" name="blob[width]" step="1" type="number" value="1024">
  <button type="submit">
    <?= i('Upload'); ?>
  </button>
</form>

So when you select a file and then submit the form, this is the data you will get in both variables:

$_FILES = [
    'blob' => [
        'error' => 0,
        'full_path' => 'example.png',
        'name' => 'example.png',
        'size' => 82884,
        'tmp_name' => '\tmp\php9gNNH6',
        'type' => 'image/png'
    ]
];

$_POST = [
    'blob' => [
        'height' => 768,
        'name' => 'example.png',
        'path' => '/tmp/php9gNNH6',
        'route' => null,
        'size' => 82884,
        'status' => 0,
        'type' => 'image/png',
        'width' => 1024
    ]
];

Again, let’s say you have an HTML structure of a form like this, which allows you to upload multiple files at once, along with the PHP code to initialize the file upload process:

<?php

if ('POST' === $_SERVER['REQUEST_METHOD']) {
    test($_FILES, $_POST);
    exit;
}

?>

<form enctype="multipart/form-data" method="post">
  <input multiple name="blobs[]" type="file">
  <input min="1" name="blobs[height]" step="1" type="number" value="768">
  <input min="1" name="blobs[width]" step="1" type="number" value="1024">
  <button type="submit">
    <?= i('Upload'); ?>
  </button>
</form>

So when you select multiple files and then submit the form, this is the data you will get in both variables:

$_FILES = [
    'blobs' => [
        'error' => [0, 0, 0, 0],
        'full_path' => ['example-1.png', 'example-2.png', 'example-3.png', 'example-4.png'],
        'name' => ['example-1.png', 'example-2.png', 'example-3.png', 'example-4.png'],
        'size' => [770638, 396104, 972387, 82884],
        'tmp_name' => ['\tmp\phpwkuZxh', '\tmp\phpIT8z1L', '\tmp\phpsBnxum', '\tmp\phpETAL9I'],
        'type' => ['image/png', 'image/png', 'image/png', 'image/png']
    ]
];

$_POST = [
    'blobs' => [
        '0' => [
            'name' => 'example-1.png',
            'path' => '\tmp\phpwkuZxh',
            'route' => null,
            'size' => 770638,
            'status' => 0,
            'type' => 'image/png'
        ],
        '1' => [
            'name' => 'example-2.png',
            'path' => '\tmp\phpIT8z1L',
            'route' => null,
            'size' => 396104,
            'status' => 0,
            'type' => 'image/png'
        ],
        '2' => [
            'name' => 'example-3.png',
            'path' => '\tmp\phpsBnxum',
            'route' => null,
            'size' => 972387,
            'status' => 0,
            'type' => 'image/png'
        ],
        '3' => [
            'name' => 'example-4.png',
            'path' => '\tmp\phpETAL9I',
            'route' => null,
            'size' => 82884,
            'status' => 0,
            'type' => 'image/png'
        ],
        'height' => 768,
        'width' => 1024
    ]
];

Example

Single file upload:

if ('POST' === $_SERVER['REQUEST_METHOD']) {
    $status = store('.\path\to\folder', $_POST['blob']);
    if (false === $status) {
        exit('File already exists.');
    }
    if (null === $status) {
        exit('Failed to upload due to file system errors.');
    }
    if (is_int($status)) {
        exit('Failed to upload with status code: ' . $status);
    }
    exit('File ' . $status . ' uploaded successfully.');
}

Multiple file uploads:

$values = [];
if ('POST' === $_SERVER['REQUEST_METHOD']) {
    foreach ($_POST['blobs'] as $k => $v) {
        if (!is_array($v)) {
            continue; // This data is not associated with the blob
        }
        $values[] = store('.\path\to\folder', $v);
    }
    test($values);
    exit;
}

Below is a list of file upload error codes according to the PHP manual:

CodeMeaning
1The uploaded file exceeds the upload_max_filesize directive in php.ini.
2The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
3The uploaded file was only partially uploaded.
4No file was uploaded.
5?
6Missing a temporary folder.
7Failed to write file to disk.
8A PHP extension stopped the file upload.

a()

Converts object to array.

all()

Checks if all items in the data pass the test.

any()

Checks if at least one item in the value passes the test.

apart()

Splits HTML/XML string into tokens.

b()

Ensures the minimum and maximum value of a value.

c()

Converts text to camel case.

c2f()

Converts class name to file name.

choke()

Allows access at certain intervals.

concat()

Concatenates multiple arrays into one array.

cookie()

Gets or sets a cookie or cookies.

d()

Loads classes automatically, efficiently.

drop()

Removes meaning-less array items to reduce the size.

e()

Evaluates string to the proper data type.

eat()

Escapes HTML/XML attribute’s value.

exist()

Checks if file/folder exists.

extend()

Merges multiple arrays into one array.

f()

Filters out characters from a string.

f2c()

Converts file name to class name.

f2p()

Converts file name to property name.

fetch()

Fetches content from a remote URL.

find()

Gets the first array item that passes the test.

fire()

Executes a callable or a function.

g()

Generates a list of files and/or folders from a folder.

ge()

Greater than or equal to.

get()

Gets values from an array using dot notation access.

h()

Hyphenates current value.

has()

Checks if an array contains a key using dot notation access.

hook()

Gets or sets a hook or hooks.

i()

Makes text translatable.

ip()

Gets the client’s IP address.

is()

Filters the data so that only items that pass the test are left.

j()

Gets array items that are not present in the second array.

k()

Generates a filtered list of files and/or folders from a folder.

l()

Converts text to lower case.

le()

Less than or equal to.

let()

Deletes values from an array using dot notation access.

long()

Converts relative URL to full URL.

lot()

Sets global variables.

m()

Normalizes range to a new range.

map()

Creates a new data set from the current data.

move()

Moves a file/folder to a folder.

n()

Normalizes string.

not()

Filters the data so that only items that does not pass the test are left.

o()

Converts array to object.

p()

Converts text to pascal case.

p2f()

Converts property name to file name.

pair()

Pairs HTML/XML attributes string as key and value in array.

path()

Normalizes and resolves file/folder path.

pluck()

Returns a new data set contains values from the key on every item.

q()

Counts the data quantity.

r()

Replaces string.

s()

Converts value to the string representation of it.

seal()

Sets a file/folder permission.

set()

Sets values to an array using dot notation access.

short()

Converts full URL to relative URL.

size()

Converts size in bytes to a human readable string format.

state()

Gets or sets a state or states.

status()

Gets current request/response headers and status or sets current response headers and status.

step()

Creates a step sequence of a split pattern.

store()

Moves the uploaded file to a folder.

stream()

Streams the file content chunk by chunk.

t()

Trims value from a delimiter once.

type()

Gets or sets current response type.

u()

Converts text to upper case.

ua()

Gets the client’s user agent string.

v()

Returns a string without the backslash prefix on every regular expression characters.

w()

Converts file name or HTML string to plain text.

x()

Returns a string with the backslash prefix on every regular expression characters.

y()

Converts iterator to array.

z()

Converts PHP values to a compact string of PHP values.

zone()

Gets or sets current application time zone.