PHP Classes

How to Implement a PHP Upload File Processor Using the Yii2 Raw File Parser: Process file uploads using the content-type header

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-04-25 (5 months ago) RSS 2.0 feedNot enough user ratingsTotal: 6 All time: 11,449 This week: 73Up
Version License PHP version Categories
yii2-rawfileparser 1.0BSD License5HTTP, PHP 5, Files and Folders
Description 

Author

This package can process file uploads using the content-type header.

It provides a request parser class for the Yii framework that can take the current HTTP request raw body data and a content type to analyze the content.

Currently, it returns the file name, content type, content size, processing error, temporary file name, and file resource.

Picture of Gabriel Alejandro López López
  Performance   Level  
Name: Gabriel Alejandro López ... <contact>
Classes: 10 packages by
Country: United States United States
Age: 35
All time rank: 4332551 in United States United States
Week rank: 18 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 3x

Documentation

Yii2 Raw File Parser

RawFileParser is a Yii2 extension that allows you to parse the content of requests that contain a raw file based on the Content-Type header. It does this by making the file available in the $_FILES array, allowing to handle it as a regular file upload.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist daxslab/yii2-rawfileparser "*"

or add

"daxslab/yii2-rawfileparser": "*"

to the require section of your composer.json file.

Configuration

In order to enable this parser you should configure [[Request::parsers]] in the following way:

return [
    'components' => [
        'request' => [
            'parsers' => [
                'application/zip' => [
                    'class' => 'daxslab\extensions\RawFileParser',
                    'basename' => 'azipfile' //optional but recommended, the name to locate the file in $_FILES
                ],
                'video/x-matroska' => 'daxslab\extensions\RawFileParser', //basename is not specified, the key $_FILES is a md5 hash of the file content. Ugly, yes...
            ],
        ],
        // ...
    ],
    // ...
];

Usage

Note: in order to the parser to be able to work: 1. The request must have the Content-Type header set to to specified value in the parser configuration 2. Yii::$app->request->getBodyParams() or Yii::$app->request->post() must be called previous to any attemp to access the file because is when the parser logic is executed.

Handling the uploaded file

Yii::$app->request->getBodyParams(); //parser is executed here, the file is on $_FILES now.
$uploadedFile = UploadedFile::getInstanceByName('azipfile');

if (!$uploadedFile) {
     throw new ServerErrorHttpException(Yii::t('app', 'No file uploaded'));
}

$uploadedFile->saveAs("/path/to/save/$uploadedFile->name");

By Daxslab.


  Files folder image Files (6)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file RawFileParser.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:6
This week:0
All time:11,449
This week:73Up