flowjs / flow-php-server

flow.js php server library, validates uploaded chunks and safely merges all chunks to a single file
http://flowjs.github.io/ng-flow/
MIT License
245 stars 42 forks source link

Work with Laravel #15

Open hugomosh opened 9 years ago

hugomosh commented 9 years ago

This is a nasty code but It work for me for an AngularJs and Laravel 5 integration. I am just happy and want to share it for sme. It is a copy from one post here but modified.

    public function getSubirArchivo($declaracion) {
        error_log('Documento 22');

        $request = new \Flow\Request();
        $config = new \Flow\Config(array(
            'tempDir' => './chunks_temp', //With write access
        ));

        $file = new \Flow\File($config, $request);
        $response = Response::make('', 200);

        $destination = './archivos/' . $declaracion . '_' . $request->getFileName();
        if ($_SERVER['REQUEST_METHOD'] === 'GET') {
            error_log('Documento GET');
            if (!$file->checkChunk()) {
                error_log('Documento GET2');
                return Response::make('', 204);
            }
        } else {
            if ($file->validateChunk()) {
                $file->saveChunk();
                error_log('Documento VaLido !!!');

            } else {
                error_log('Documento INVALIDO');
                // error, invalid chunk upload request, retry
                return Response::make('Error in chunck', 400);
            }
        }
        if ($file->validateFile() && $file->save($destination)) {
            error_log('EXITO!');
            $response = Response::make('pass some success message to flow.js', 200);
        }
        return $response;
    }
citosid commented 7 years ago

@hugomosh we are using the same thing you have here, but with PHP 7, and we are seeing very weird things.

We have nginx in front of our php, using php-fpm, but we noticed that it has to retry a lot of chunks several times before uploading the files... after hours of debugging and testing, we found that if we add a sleep(2); it just works... so, we were wondering, have you experience the same thing? Do you have any clue of what could be the problem?

hugomosh commented 7 years ago

@citosid sorry, I don't longer work in the project where I used it.

citosid commented 7 years ago

Thanks @hugomosh... it turned out to be a problem with fpm and permissions... after setting the permissions of the folders you need to restart fpm, otherwise it will not work.