1. Go to this page and download the library: Download shetabit/chunky library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shetabit / chunky example snippets
# On the top of the file.
use Shetabit\Chunky\Classes\StreamOut;
...
$path = '../path/to/yourfile.mp3';
$stream = new StreamOut($path);
$stream->process();
$stream->process($request->header('Range'));
$delay = 1000; // wait a millisecond after every buffer
$bufferSize = 16 * 1024; // write 16 KB at a time
$stream = new StreamOut($path, $delay, $bufferSize);
# On the top of the file.
use Shetabit\Chunky\Classes\StreamIn;
...
$inputName = 'media'; // html input's name
$uploadPath = './path/to/address';
$inputStream = new StreamIn($inputName, $uploadPath);
$uploads = $inputStream->process();
# On the top of the file.
use Shetabit\Chunky\Classes\ChunkedUploadCollector;
use Shetabit\Chunky\Classes\StreamIn;
...
$stream = new StreamIn($inputName, $uploadPath, new ChunkedUploadCollector($inputName));
$uploads = $stream->process();
new ChunkedUploadCollector($inputName, ['range', 'size', 'name', 'mime']);
new ChunkedUploadCollector($inputName, ['range' => 'chunk_range', 'size' => 'total_bytes']);
# On the top of the file.
use Shetabit\Chunky\Classes\StreamIn;
use Shetabit\Chunky\Exceptions\ValidationFailedException;
use Shetabit\Chunky\Validations\Extension;
use Shetabit\Chunky\Validations\MimeType;
use Shetabit\Chunky\Validations\Size;
...
$stream = new StreamIn($inputName, $uploadPath, validations: [
new Size('5M', '1K'), // nothing over 5 MB, nothing under 1 KB
new Extension(['jpg', 'jpeg', 'png', 'gif']),
new MimeType(['image/jpeg', 'image/png', 'image/gif']),
]);
try {
$uploads = $stream->process();
} catch (ValidationFailedException $exception) {
// every single thing that is wrong, one message per validation that failed
$errors = $exception->getErrors();
}
use Shetabit\Chunky\Contracts\FileInterface;
use Shetabit\Chunky\Contracts\ValidationInterface;
use Shetabit\Chunky\Exceptions\ValidationFailedException;
class NotOnAWeekend implements ValidationInterface
{
public function validate(FileInterface $file): void
{
if (in_array(date('N'), ['6', '7'], true)) {
throw new ValidationFailedException('Come back on monday.', $file);
}
}
}
$stream
->beforeValidate(function (TempFileInterface $file): void {
// before the chunk is checked
})
->afterValidate(function (TempFileInterface $file, array $errors): void {
// after it was checked, whether it passed or not
})
->beforeUpload(function (TempFileInterface $file, string $path): void {
// before the chunk is written, and where it is written to
})
->afterUpload(function (TempFileInterface $file, string $path, int $assembled): void {
// after it was written, and how much of the file is there now
});
$stream->afterUpload(function (TempFileInterface $file, string $path, int $assembled): void {
if ($assembled === (int) $file->size) {
// the file is whole: put it in the database, hand it to a queue, ...
}
});
# On the top of the file.
use Shetabit\Chunky\Classes\Collector;
...
// html: <input name="media" type="file">
$inputName = 'media';
$collector = new Collector($inputName);
// collect all input files
$files = $collector->collect();
$file = $files[0]; // retrieve the first file
// you can store each file like the below
$path = './path/to/filename.jpg';
$file->saveAs($path); // save file as filename.jpg
// or we can use file's original name
$path = './path/to/'.$file->name;
$file->saveAs($path);
// write the first 1024 bytes of the chunk at byte 4096 of the destination
$file->saveAs($path, 4096, 1024);
namespace App\Http\Controllers;
use App\Models\File;
use Illuminate\Http\Request;
use Shetabit\Chunky\Classes\StreamOut;
class StreamOutController extends Controller
{
/**
* Stream file output
*/
public function __invoke(Request $request, File $file): void
{
// proceed until all of the file has been sent to the client
ini_set('max_execution_time', '0');
// retrieve file's path
$path = '../'.$file->path;
// prepare stream
$stream = new StreamOut($path);
// run stream
$stream->process($request->header('Range'));
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Shetabit\Chunky\Classes\Collector;
use Shetabit\Chunky\Classes\StreamIn;
class StreamInController extends Controller
{
/**
* Stream file input
*/
public function __invoke(Request $request): void
{
/**
if you want simple file upload (not resumable and chunk)
you can use the below code
**/
$inputName = 'media';
$collector = new Collector($inputName);
// collect all input files
$files = $collector->collect();
$file = $files[0]; // retrieve the first file
// you can store each file like the below
$path = './path/to/filename.jpg';
$file->saveAs($path); // save file as filename.jpg
// or we can use file's original name
$path = './path/to/'.$file->name;
$file->saveAs($path);
// ---------------------------------------------
/**
if you want advanced file upload (resumable and chunk)
you can use the below code
**/
$inputName = 'media'; // html input's name
$uploadPath = './path/to/address';
$inputStream = new StreamIn($inputName, $uploadPath);
$uploads = $inputStream->process();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.