public function splitFile($dstDir, $chunkSize = 2048)
    {
        $dstDir = rtrim($dstDir, DIRECTORY_SEPARATOR);

        $srcFile = fopen($this->filePath, 'r');
        $totalSize = filesize($this->filePath);
        $chunksNo = ceil($totalSize / $chunkSize);
        $lastChunkSize = $totalSize % $chunkSize;

        // Chunk must be the same size as requested but the last chunk can be up to $chunkSize * 2
        if ($lastChunkSize > 0 && $lastChunkSize < $chunkSize) {
            $chunksNo -= 1;
        }

        $chunkNames = [];
        $uniqueChunkName = uniqid();

        for ($x = 1; $x <= $chunksNo; ++$x) {
            // For the last chunk we send all bytes left
            if ($x == $chunksNo) {
                $chunkSize = -1;
            }

            $chunkName = $uniqueChunkName.'_'.$x;
            $chunkNames[] = $chunkName;

            $dstFilePath = $dstDir.DIRECTORY_SEPARATOR.$chunkName;
            $dstFile = fopen($dstFilePath, 'wb');

            stream_copy_to_stream($srcFile, $dstFile, $chunkSize);
            fclose($dstFile);
        }

        fclose($srcFile);

        return $chunkNames;
    }
public function splitFile($dstDir, $chunkSize = 2048)
    {
        $dstDir = rtrim($dstDir, DIRECTORY_SEPARATOR);

        $srcFile = fopen($this->filePath, 'r');
        $totalSize = filesize($this->filePath);
        $chunksNo = ceil($totalSize / $chunkSize);
        $lastChunkSize = $totalSize % $chunkSize;

        // Chunk must be the same size as requested but the last chunk can be up to $chunkSize * 2
        if ($lastChunkSize > 0 && $lastChunkSize < $chunkSize) {
            $chunksNo -= 1;
        }

        $chunkNames = [];
        $uniqueChunkName = uniqid();

        for ($x = 1; $x <= $chunksNo; ++$x) {
            // For the last chunk we send all bytes left
            if ($x == $chunksNo) {
                $chunkSize = -1;
            }

            $chunkName = $uniqueChunkName.'_'.$x;
            $chunkNames[] = $chunkName;

            $dstFilePath = $dstDir.DIRECTORY_SEPARATOR.$chunkName;
            $dstFile = fopen($dstFilePath, 'wb');

            stream_copy_to_stream($srcFile, $dstFile, $chunkSize);
            fclose($dstFile);
        }

        fclose($srcFile);

        return $chunkNames;
    }
public function splitFile ( $ dstDir , $ chunkSize = 2048 ) { $ dstDir = rtrim ( $ dstDir , DIRECTORY_SEPARATOR ) ; $ srcFile = fopen ( $ this -> filePath , 'r' ) ; $ totalSize = filesize ( $ this -> filePath ) ; $ chunksNo = ceil ( $ totalSize / $ chunkSize ) ; $ lastChunkSize = $ totalSize % $ chunkSize ; // Chunk must be the same size as requested but the last chunk can be up to $chunkSize * 2 if ( $ lastChunkSize > 0 && $ lastChunkSize < $ chunkSize ) { $ chunksNo -= 1 ; } $ chunkNames = [ ] ; $ uniqueChunkName = uniqid ( ) ; for ( $ x = 1 ; $ x <= $ chunksNo ; ++ $ x ) { // For the last chunk we send all bytes left if ( $ x == $ chunksNo ) { $ chunkSize = - 1 ; } $ chunkName = $ uniqueChunkName . '_' . $ x ; $ chunkNames [ ] = $ chunkName ; $ dstFilePath = $ dstDir . DIRECTORY_SEPARATOR . $ chunkName ; $ dstFile = fopen ( $ dstFilePath , 'wb' ) ; stream_copy_to_stream ( $ srcFile , $ dstFile , $ chunkSize ) ; fclose ( $ dstFile ) ; } fclose ( $ srcFile ) ; return $ chunkNames ; }
Split file into chunks and save it to destination.

@param string $dstDir    The destination folder path
@param int    $chunkSize

@return array The array of chunk names
Split file into chunks and save it to destination .