Gargron / fileupload

PHP FileUpload library that supports chunked uploads
MIT License
459 stars 87 forks source link

Chucked upload not working. #50

Closed mcfriend99 closed 7 years ago

mcfriend99 commented 7 years ago

Only the first chance in a chucked upload gets uploaded. Here is my code:

$extension = pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);

    // Simple validation (max file size 2MB and only two allowed mime types)
    $validator = new FileUpload\Validator\Simple('200M', [
        'video/x-flv', // .flv
        'video/mp4', // .mp4
        'application/x-mpegURL', // iphone .mp4, .mpu
        'video/MP2T', // iphone .ts
        'video/3gpp', // .3gp videos
        'video/3gpp2', // .3gp, .3gp2, .3gpp videos
        'video/quicktime', // .mov
        'video/x-msvideo', // .avi
        'video/webm', // .webm
        'video/webm', // .webm
        'video/ogg', // .ogg
        'video/x-matroska', // .mkv
        'video/x-ms-wmv' // .wmv
    ]);

    // Simple path resolver, where uploads will be put
    $pathresolver = new FileUpload\PathResolver\Simple('../../contents/videos/');

    // The machine's filesystem
    $filesystem = new FileUpload\FileSystem\Simple();

    // FileUploader itself
    $fileupload = new FileUpload\FileUpload($_FILES['video'], $_SERVER);

    $filename = time() . preg_replace('/[^a-zA-Z0-9_]+/', '_',  preg_replace('/[.]' .$extension. '$/', '', trim($_FILES['video']['name'])));
    $filenamegenerator = new FileUpload\FileNameGenerator\Custom($filename  . '.mp4');

    // Adding it all together. Note that you can use multiple validators or none at all
    $fileupload->setPathResolver($pathresolver);
    $fileupload->setFileSystem($filesystem);
    $fileupload->addValidator($validator);
    $fileupload->setFileNameGenerator($filenamegenerator);

    /*// Doing the deed
    list($files, $headers) = $fileupload->processAll();

    // Outputting it, for example like this
    foreach($headers as $header => $value) {
        header($header . ': ' . $value);
    }*/

    //echo json_encode(['files' => $files]);

    $fileupload->addCallback('completed', function(FileUpload\File $file) {
echo $file->getRealPath();
});

Getting Internerla Server error issue when the file size is higher than my maxChunckSize.

Here is my Javascript:

$('#fileupload').fileupload({
    maxChunkSize: 10000000, // 10 MB
    add: function (e, data) {
        var that = this;
        $.getJSON('server/php/', {file: data.files[0].name}, function (result) {
            var file = result.file;
            data.uploadedBytes = file && file.size;
            $.blueimp.fileupload.prototype
                .options.add.call(that, e, data);
        });
    }
});
mcfriend99 commented 7 years ago

Is it even working the example code in https://github.com/blueimp/jQuery-File-Upload/wiki/Chunked-file-uploads?

Paradinight commented 7 years ago

https://github.com/Gargron/fileupload/issues/47#issuecomment-287022905

The mime type validation does not work.

mcfriend99 commented 7 years ago

Don't understand you. Are you saying I should simply implement my own validation?

adelowo commented 7 years ago

@mcfriend99, @Paradinight is right.. There's no way the mime type validator would work with chucked uploads (the second time the upload is restarted ) because partially uploaded files usually have an invalid mime type since they are incomplete .. Hence validation would fail.

Resuming a partially uploaded file is a tradeoff between "usability and security "

adelowo commented 7 years ago

*chunked

Paradinight commented 7 years ago

adelowo you could check the $_FILE mime type, if incorrect throw an error. After complete upload check the mime type again, but this time with php. The First check is for the usability and the second for the security.